0
votes

So I have an issue with Factory Girl, I've been using it quite a bit and this is the first Rails app that I've had this issue. Basically what's happening is when I use it in the console, it crashes with the message /home/vagrant/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/pry-0.10.3/lib/pry/last_exception.rb:54:in 'bt_source_location_for': undefined method '[]' for nil:NilClass (NoMethodError) and a backtrace that consists entirely of gem libraries. When I go to test, it also fails. In the test initializer support directory in spec/support/factory_girl.rb I included the appropriate code to lint the factories that I always do with DatabaseCleaner:

RSpec.configure do |config|
  config.before(:suite) do
    begin
      DatabaseCleaner.start
      FactoryGirl.lint
    ensure
      DatabaseCleaner.clean
    end
  end
end

This causes an error when the tests are run: /home/vagrant/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.6.3/lib/spring/application.rb:271:in 'ensure in block (2 levels) in shush_backtraces': undefined method 'reject!' for nil:NilClass (NoMethodError)

When I remove that file a new error is thrown when I first go to use the Factory Girl methods:

ActiveRecord::Base.transaction do
  create(:team)
end

-

NoMethodError: undefined method `reject!' for nil:NilClass
--- Caused by: ---
fatal:
  exception reentered

I've never seen the 'Caused by: error reentered message' before but it looks pretty cryptic. The version of Factory Girl is 4.5, Rails version 4.2.5, RSpec version 3.4. I just started this app so there isn't a whole lot of other code to the app but here are the rails helper, spec helper, and Gemfile.

rails_helper.rb

# This file is copied to spec/ when you run 'rails generate
rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'shoulda/matchers'
require 'simplecov'
SimpleCov.start 'rails'

include ActionDispatch::TestProcess
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
end

spec_helper.rb

require 'paperclip/matchers'
require 'devise'

RSpec.configure do |config|
  config.include Paperclip::Shoulda::Matchers

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

Gemfile

source 'https://rubygems.org'

gem 'coffee-rails', '~> 4.1.0'
gem 'devise'
gem 'jbuilder', '~> 2.0'
gem 'jquery-rails'
gem 'jwt'
gem 'paperclip', '~> 4.3'
gem 'pg', '~> 0.15'
gem 'rails', '~> 4.2.5'
gem 'sass-rails', '~> 5.0'
gem 'turbolinks'
gem 'uglifier', '>= 1.3.0'
gem 'unicorn'

group :test, :development do
  gem 'dotenv-rails'
  gem 'bullet'
  gem 'did_you_mean', '~> 0.10.0'
  gem 'factory_girl_rails'
  gem 'pry-rails'
  gem 'rspec-rails'
end

group :test do
  gem 'database_cleaner'
  gem 'shoulda-matchers', require: false
  gem 'simplecov', :require => false
end

group :development do
  gem 'active_record_query_trace'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'spring'
  gem 'spring-commands-rspec'
end
1

1 Answers

0
votes

I found the issue. In my factory I referenced an association from the has many sign where it's only supposed to be on the belongs to side. Not a great error message for that though.

FactoryGirl.define do
  factory :user do
    association :team
  end

  factory :team do
    association :user
  end
end

class User < ActiveRecord::Base
  belongs_to :team
end

class Team < ActiveRecord::Base
  has_many :users
end

The team should not have the association line.