Aethelflaed / mongoid-fixture_set

Fixtures for Mongoid
MIT License
7 stars 15 forks source link

Using Mongoid::FixtureSet with RSpec #6

Open PandaWhisperer opened 8 years ago

PandaWhisperer commented 8 years ago

I want to use this gem with RSpec, but can't quite figure out how. So far, I have this in my spec_helper.rb:

require 'mongoid-fixture_set'

RSpec.configure do |config|
  config.include Mongoid::FixtureSet::TestHelper
end

I'm not really sure how to set the fixture_path, however.

Aethelflaed commented 8 years ago

Have you tried config.fixture_path ?

PandaWhisperer commented 8 years ago

@Aethelflaed I haven't. Just tried it and it gives me the following error:

undefined method `fixture_path=' for #<RSpec::Core::Configuration:0x007f91bc8370e8> (NoMethodError)
PandaWhisperer commented 8 years ago

Aha. It seems to work when I use this instead:

require 'mongoid-fixture_set'

RSpec.configure do |config|
  config.include Mongoid::FixtureSet::TestHelper
  config.add_setting(:fixture_path, default: "#{Rails.root}/spec/fixtures")
end

However, that still doesn't make the fixtures available. I either need to call setup_fixtures myself, or add this to the configuration:

  config.before(:each) { setup_fixtures }
  config.after(:each) { teardown_fixtures }
PandaWhisperer commented 8 years ago

After managing to get Mongoid::FixtureSet set up with RSpec as above, when running my tests, RSpec gets stuck for an extremely long time (30+ seconds) on the loading fixtures step, even though I've only defined a single, very minimal fixture file, spec/fixtures/currencies.yml:

usd:
  label: US Dollar
  code: USD
  status: active
  created_by: 2
  updated_by: 2

euro:
  label: Euro
  code: EUR
  status: active
  created_by: 2
  updated_by: 2

After the long wait, it eventually fails with the following error:

TypeError:
    no implicit conversion of nil into String
# ~/.rvm/gems/ruby-2.2.4/gems/mongoid-fixture_set-1.5.0/lib/mongoid/fixture_set.rb:69:in `join'
# ~/.rvm/gems/ruby-2.2.4/gems/mongoid-fixture_set-1.5.0/lib/mongoid/fixture_set.rb:69:in `block in create_fixtures'
# ~/.rvm/gems/ruby-2.2.4/gems/mongoid-fixture_set-1.5.0/lib/mongoid/fixture_set.rb:64:in `map'
# ~/.rvm/gems/ruby-2.2.4/gems/mongoid-fixture_set-1.5.0/lib/mongoid/fixture_set.rb:64:in `create_fixtures'
# ~/.rvm/gems/ruby-2.2.4/gems/mongoid-fixture_set-1.5.0/lib/mongoid/fixture_set/test_helper.rb:91:in `load_fixtures'
# ~/.rvm/gems/ruby-2.2.4/gems/mongoid-fixture_set-1.5.0/lib/mongoid/fixture_set/test_helper.rb:75:in `setup_fixtures'
# ./spec/routes/categories_spec.rb:41:in `block (2 levels) in <top (required)>'
PandaWhisperer commented 8 years ago

Turns setting fixture_path using config.add_setting was actually not setting the fixture path correctly so that Mongoid::FixtureSet could find it. Therefore, the test helper ends up scanning the entire file system for .yml files.

This works:

RSpec.configure do |config|
  config.before(:all) do
    self.class.fixture_path = "#{Rails.root}/spec/fixtures"
  end
end
Aethelflaed commented 8 years ago

glad you found a way to work around it, need any other help? Sorry for late reply, I took a holiday and wasn't much connected