Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-plugins

Schema and fixtures not being loaded when running rake:test from new Rails plugin


I am creating a brand new full gem plugin using Rails 3.1.0 on ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]. I am having trouble with the schema and fixtures not being loaded for running rake test. Following are the steps I am taking:

Create the plugin:

rails plugin new core --full

From the plugin, generate a new scaffold:

rails g scaffold user

Run the db create and migrate:

rake db:create
rake db:migrate

Run the tests:

rake test

When running the functional tests, I am receiving a set of error like the following from the controller tests:

1) Error:
test_should_create_user(UsersControllerTest):
NoMethodError: undefined method `users' for #<UsersControllerTest:0x00000003babca8>

This error seems to stem from the fact that the test doesn't seem to understand the fixture call. It doesn't look like the test db is getting the schema loaded or having the fixtures loaded. Is this expected behavior? Is there something I'm missing in this scenario? All the fixtures are there. Is there some process I need to follow to get these tests to run correctly?


Solution

  • OK, I think I've made some progress. It looks like I can resolve it with the following changes.

    I've added the following to test_helper:

    #Run any available migration
    ActiveRecord::Migrator.migrate 'up'
    
    # Set fixtures root
    ActiveSupport::TestCase.fixture_path=(File.expand_path("../fixtures",  __FILE__))
    ActiveSupport::TestCase.fixtures :all
    

    This allows the test environment to find my fixtures outside of the dummy application.

    After this change, I was still having an issue where the test database was not being prepared for the test. I resolved this by dropping into the test/dummy directory and running:

    rake db:test:prepare
    

    After this, I am able to run "rake test" successfully. It's somewhat a pain to have to drop in to the dummy app to prepare the db. There has to be a way around this. Any suggestions?