0

I am working on migrating the test suite for my project to rspec. I am getting the following error trying to access my fixtures:

undefined method `[]' for nil:NilClass

Here is an example spec file:

require 'rails_helper'
feature "edit contacts" do

  fixtures :contacts

  before(:all) do
    @thing = contacts(:two)
  end

  scenario "do a thing" do
    visit new_contact_path
  end
end

I get the error in the before block, on the call to contacts(:two)

Here is config info from rails_helper.rb

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
end

Here is version info from Gemfile.lock:

rails (4.0.13)
rspec-core (3.2.2)
rspec-expectations (3.2.0)
rspec-mocks (3.2.1)
rspec-rails (3.2.1)
rspec-support (3.2.2)

I'm happy to provide any additional supporting information if needed. Thanks for taking a look, any help appreciated!

Fred Willmore
  • 4,386
  • 1
  • 27
  • 36
  • Does `YAML.load(File.read(Rails.root.join('spec','fixtures.yml')))` return valid YAML content? (something like a big hash) – MrYoshiji Apr 06 '15 at 21:18

1 Answers1

0

ok - finally resolved this with the help of a coworker. The issue was that I should have been using before(:each) instead of before(:all).

Apparently, using :each triggers a 'before_setup' callback in ActiveRecord::FixtureSet which sets up all the fixtures, and :all doesn't.

According to the docs:

before(:each) blocks are run before each example before(:all) blocks are run once before all of the examples in a group

Fred Willmore
  • 4,386
  • 1
  • 27
  • 36