jeudi 26 mars 2015

Upgrading to Rails 4.1.5 leads to FactoryGirl::InvalidFactoryError w/ FactoryGirl.lint (Mongoid embedded docs)

I'm incrementally upgrading an app from Rails 4.1.4 to 4.2.1. I started by changing the Rails version to 4.1.5 in Gemfile and then running bundle update.


Before that update, all of my specs were passing. Afterwards, the test suite immediately blows up because of the following error:



/home/vagrant/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/linter.rb:14:in `lint!': The following factories are invalid: (FactoryGirl::InvalidFactoryError)
* csv_test_file -
Problem:
Cannot persist embedded document CsvTestFile without a parent document.
Summary:
If the document is embedded, in order to be persisted it must always have a reference to its parent document. This is most likely caused by either calling CsvTestFile.create or CsvTestFile.create! without setting the parent document as an attribute.
Resolution:
Ensure that you've set the parent relation if instantiating the embedded document directly, or always create new embedded documents via the parent relation. (Mongoid::Errors::NoParent)


There is a model CsvTestFile:



class CsvTestFile
include Mongoid::Document

embedded_in :org
end


...and Org:



class Org
include Mongoid::Document

embeds_many :csv_test_files, cascade_callbacks: true
end


I've searched the codebase pretty thoroughly and I don't think CsvTestFile factories are ever instantiated directly. It is used in the Org factories like so:



FactoryGirl.define do
factory :org do
# some stuff
factory :org_with_csv_with_valid_encounters do
csv_test_files [FactoryGirl.build(:csv_with_valid_encounters)]
end
end
end


Here the instance of CsvTestFile instance is being built as a child document of the Org instance.


There are various types of CsvTestFile factories (valid encounters, invalid encounters, etc) and factories for Org that use these different CsvTestFile factories. All of these are failing during FactoryGirl.lint when I run the test suite.


So far it looks like I can avoid throwing an error for each CsvTestFile factory if I add something like this:



factory :csv_with_valid_encounters
filename 'filename.csv'
contents 'csv,content,here'
org # <------- add this line
end


Based on the original error output, though, it seems like this shouldn't be necessary because the only time the test suite uses these is when they're being instantiated within a parent Org's factory. I guess it's FactoryGirl.lint itself that is instantiating a single CsvTestFile factory and finding that it lacks a parent.


I'm not sure if it's something about RSpec or Factory Girl or something else that is causing this to break now. Does anyone know what might be causing this to happen after the update when it was fine before, and if there's anything I can do to avoid it besides adding org as an attribute to every CsvTestFile factory?


Aucun commentaire:

Enregistrer un commentaire