0

I have a ruby app that uses ActiveRecord and sqllite. I am trying to write tests but I get this error:

 Failure/Error: user = described_class.create(name: name)

     ActiveRecord::StatementInvalid:
       Could not find table 'users'

This is my gemfile:

source "https://rubygems.org"

gem "sinatra-activerecord"
gem "sqlite3"

group :test do
  gem 'database_cleaner'
end

group :test, :development do
  gem "rspec-rails", ">= 2.1.0"
  gem "pry"
end

I have a spec_helper that looks like this:

RSpec.configure do |config|


  ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/test.db')
end

What can I do to create a test database and run the migrations for my sqllite tests?

2 Answers 2

3

bin/rails RAILS_ENV=test db:migrate and/or bin/rails db:test:prepare will update your test db. You also might want to check out the Rails Testing Guides too - lots of helpful info there. Or if you want to avoid fixtures, FactoryBot is great for generating test data.

4
  • 2
    Ginnie's answer here is perfect, except that the OP doesn't specify the app uses Rails. ActiveRecord tasks/migrations are a great solution to the question being asked, and can be done outside of Rails (though it requires some extra tinkering). Toss rails into the Gemfile for an easy solution, or give the migrations guide a good read-through to better understand the api: guides.rubyonrails.org/active_record_migrations.html
    – Sean Huber
    Commented Jun 1, 2018 at 3:04
  • Good point, Sean Huber, I didn't notice the sinatra gem. I don't use Sinatra much. Would the "rspec-rails" gem be expect to work with Sinatra? Commented Jun 1, 2018 at 3:21
  • Well from the runtime dependencies listed in rspec-rails, github.com/rspec/rspec-rails/blob/master/rspec-rails.gemspec, I see runtime dependencies of activesupport, actionpack, and railties. I'm sure you could set up a project with just sinatra and these rails components and have rspec-rails + active record migrations work. But in my opinion if you bring in all these rails gems, you might as well just build your app on top of rails instead of piecing it all together yourself. Good question.
    – Sean Huber
    Commented Jun 1, 2018 at 4:11
  • Also db:test:prepare was deprecated in 4.1 and has since been removed completely Commented Jun 1, 2018 at 14:41
2

Ginnie's answer can work, but if like Sean mentionned you need an ActiveRecord only solution instead of using Rails consider this :

I recently developed a gem to bundle some ActiveRecord models without using Rails. Here is what I did to test my models with rspec:

spec/spec_helper.rb:

ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
                                        database: ':memory:')
ActiveRecord::Schema.define do
  require_relative '../lib/db/migrate/create_models'
end

lib/db/migrate/create_models.rb: (to make this file I copy/pasted the actual schema.rb generated by migrations)

class CreateModels < ActiveRecord::Migration[5.1]
 create_table "users", force: :cascade do |t|
   t.string "uid", default: "", null: false
   t.string "email", null: false
   // other attributes
 end
 // other tables
end

This will create your needed tables for your tests to run.

Don't forget to require spec_helper in your *_spec.rb file and you're good to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.