Karan
Karan

Reputation: 15094

ruby on rails 4: could not find table 'table_name' even after a rake db:test:prepare

I receive the following error, even after doing a rake db:test:prepare. I am running rails 4.0.

1) Core::PostsController GET index assigns all posts as @posts
     Failure/Error: post = Post.create! valid_attributes
     ActiveRecord::StatementInvalid:
       Could not find table 'core_posts'
     # ./spec/controllers/core/posts_controller_spec.rb:36:in `block (3 levels) in <module:Core>'

I am running this test inside an engine, so could it be something related? My test looks like this:

module Core
  describe PostsController do

    # This should return the minimal set of attributes required to create a valid
    # Post. As you add validations to Post, be sure to
    # adjust the attributes here as well.
    let(:valid_attributes) { {  } }

    # This should return the minimal set of values that should be in the session
    # in order to pass any filters (e.g. authentication) defined in
    # PostsController. Be sure to keep this updated too.
    let(:valid_session) { {} }

    describe "GET index" do
      it "assigns all posts as @posts" do
        post = Post.create! valid_attributes
        get :index, {}, valid_session
        assigns(:posts).should eq([post])
      end
    end


  end
end

Any ideas? Thanks!

Upvotes: 1

Views: 1948

Answers (2)

CuriousMind
CuriousMind

Reputation: 34135

cd into the engine dir & generate a dummy app for testing for your engine:

rails plugin new . --full --mountable --dummy-path spec/dummy

the above command will generate a full mountable engine with isolated namespace, meaning that all the controllers and models from this engine will be isolated within the namespace of the engine. For instance, the Post model later will be called Core::Post, and not simply Post. Since you have already generated the app -- incase of conflicts, you can skip the change.

Further, engine comes with a dummy application, located at spec/dummy because we told it to do that with the --dummy_path option. This dummy application is just a bare-bones Rails application that can be used to test the engine as if it was mounted inside a real application.

Then, you need to change rspec to use this dummy app, by making following changes:

inside spec/spec_helper.rb change this line

require File.expand_path("../../config/environment", __FILE__)

to this

require File.expand_path("../dummy/config/environment",__FILE__)

As config/environment.rb file doesn’t live two directories up, but rather inside spec/dummy.

Now, you can run the migration by following command.

RAILS_ENV=test bin/rake db:migrate

Why not db:test:prepare?

We cannot run rake db:test:prepare because it is unavailable. This db:migrate task is especially altered for engines, and will run the migrations for the engine PLUS migrations within the spec/dummy/db folder.

Upvotes: 2

Benjamin Bouchet
Benjamin Bouchet

Reputation: 13181

Try to recreate your test DB

without rbenv

RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:test:prepare

with rbenv

RAILS_ENV=test bundle exec rake db:drop
RAILS_ENV=test bundle exec rake db:create
RAILS_ENV=test bundle exec rake db:test:prepare

Upvotes: 0

Related Questions