RSpec failure: could not find table after migration...?
Solution 1
Try to execute
rake db:test:prepare
This should fix your tests db.
Solution 2
The point here is that rspec
command doesn't execute migrations on your test database. and rake db:migrate
only runs migrations in your current environment, probably development
. Others environment like production
and test
ends without having those changes.
You can run
rake spec
That will prepare your testing db (drop and create using schema.rb
) and run all tests.
As the other answer suggested, this:
rake db:test:prepare
Will also setup your testing db, but you have to run the rspec command after that, so, personally I prefer the first option.
Comments
-
neezer almost 2 years
I have a naked rails 3 app with one model, generated using
rails g model User
.I've added a factory (using
factory_girl_rails
):Factory.define :user do |f| f.email "[email protected]" f.password "blah" f.password_confirmation "blah" f.display_name "neezer" end
Then I've added one test:
require 'spec_helper' describe User do subject { Factory :user } it "can be created from a factory" do subject.should_not be_nil subject.should be_kind_of User end end
Then I migrate my database using
rake db:migrate
.Then I run the test using
rspec spec
, and the test fails with the following:Failures: 1) User can be created from a factory Failure/Error: subject { Factory :user } ActiveRecord::StatementInvalid: Could not find table 'users' # ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>' # ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'
I'm confused, because I did just migrate my database, and my
schema.db
file reflects that there is a users table present, so what gives?I know this is a beginner question, but banging my head against a wall isn't working...
factory_girl (1.3.3) factory_girl_rails (1.0.1) rails (3.0.5) rspec-rails (2.5.0) sqlite3 (1.3.3)