RSpec failure: could not find table after migration…?

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)

Rspec and Devise: Could not find a valid mapping for #

using RSpec and Devise, I’m getting a mapping error. 1) BusinessesController GET index sets @new_vacation Failure/Error: sign_in @user RuntimeError: Could not find a valid mapping for #<User id:

Core Data Lightweight Migration failure – “table ZTABLENAME already exists”

NB! I have seen Core Data Migration – Table Already Exists, and it did not solve my problem. I’m trying to do a lightweight migration of my data model, and am getting a failure at this point: NSDictio

Could not find generator rspec:install.

I’m trying to follow this tutorial here: http://railstutorial.org/chapters/static-pages#top When I run: $ rails generate rspec:install I get: Could not find generator rspec:install. What could be

Could not find pg-0.16.0 in any of the sources (Bundler::GemNotFound) in Rspec after installing postgres

I installed postgres using homebrew and it worked fine – but now rspec won’t run my tests and is throwing this error: Could not find pg-0.16.0 in any of the sources (Bundler::GemNotFound) the relevan

Could not find generator rspec_model issue

I am trying to create an rspec for my model User but every time I run: rails g rspec_model User I get Could not find generator rspec_model. I checked another stackoverflow thread and it said make sur

rails – Could not find generator rspec:install.

I don’t know it can’t find the generator. gem install rspec Fetching: rspec-core-2.8.0.gem (100%) Fetching: rspec-expectations-2.8.0.gem (100%) Fetching: rspec-mocks-2.8.0.gem (100%) Fetching: rspec-2

Could not find generator rspec:install. Rails4.0.5 ruby2.0.0

I cant install rspec. command: rails generate rspec:install ↓ result: Could not find generator rspec:install. ruby ‘2.0.0’ #ruby-gemset=railstutorial_rails_4_0 gem ‘rails’, ‘4.0.5’ group :development,

Could not find table ‘types’ during Type.new call in RSpec test

I am having a problem where RSpec is crashing out when I endeavour to run a model test on the app I have recently inherited. My test database is configured like this: test: adapter: sqlite3 database:

Could not find generator rspec:install

I’m running ruby 1.8.7 and rails 3.0.3. And I can’t get rspec installed. My Gemfile contains the following: group :test, :development do gem rspec-rails, ~> 2.4 end But I there is no rspec:ins

Could not find table

I don`t understand why I am getting the error: could not find table libraryusers10s In my code below, I define two tables library_users10 and library_books10 , and associate them with classes. The fo

Answers

Try to execute

rake db:test:prepare

This should fix your tests db.

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.

try this out:

For rails version > 4.1+ this solution will work as the current scenario.

but in Rails 4.1+, rake db:test:prepare is deprecated.

try using

rake db:migrate RAILS_ENV=test (it will work for all version of rails)