Search code examples
ruby-on-railsmongoidautomated-testsscaffolding

Mongoid scaffolding tests fail on fixtures


When I create a new rails app with mongoid:

rails new tddapp --skip-active-record
cd tddapp
echo "gem 'mongoid'" >> Gemfile
bundle
rails g mongoid:config

And then generate a simple scaffold:

rails g scaffold building height:Integer name:String

The scaffolding is created and the minimal app runs just fine. But the scaffold-generated tests fail with:

NoMethodError: undefined method `buildings' for #<BuildingsControllerTest:0x007fa6afbf78d8>

The offending line shows that the test controller can't find the fixtures:

@building = buildings(:one)

The mongoid generator was nice enough to create fixtures in test/fixtures/buildings.yml indicating some level of cooperation with the whole idea of functional tests for the scaffold.

Is there some configuration setting or something I need to change to get the functional tests to load mongoid's fixtures? Or are the scaffolding functional tests expected to fail with mongoid? (And if so, why does mongoid bother creating fixture files?)


Solution

  • Not for personal promotion, but because I got stuck on this problem several times and because I think it may helps many other people out there, I created mongoid-fixture_set, which is available on github.

    It works more or less the same as ActiveRecord::FixtureSet, as I ported a lot of the code, you just have to tie it yourself to the test class:

    class ActiveSupport::TestCase
      include Mongoid::FixtureSet::TestHelper
      self.fixture_path = "#{Rails.root}/test/fixtures"
    end
    

    The same thing is done for ActiveRecord by rails in the gem railties in 'lib/rails/test_help.rb'