1

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!

6
  • Did you actually create the table? Commented Aug 2, 2013 at 7:24
  • i presume rake db:test:prepare created it ... Commented Aug 2, 2013 at 7:25
  • Only if it exists in your development database and your schema file. Rails isn't psychic. Commented Aug 2, 2013 at 7:26
  • yeah, so I did rails db:migrate first, and then rails db:test:prepare. The schema does have: create_table "core_posts", force: true do |t| t.datetime "created_at" t.datetime "updated_at" end Commented Aug 2, 2013 at 7:52
  • I just realised that my development.sqlite3 in my main app has data, but the development.sqlite3 inside my engine is empty. I am running rspec from inside my engine. I presume it is because of that? Commented Aug 2, 2013 at 7:54

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

env RAILS_ENV=test rake db:migrate
after the migrations, where can I run test (bundle exec rspec spec) ? in the engine root directory or in dummy? Thanks
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.