Unclear when to use specific FactoryGirl syntax

In the latest version of FactoryGirl, some syntax methods, such as Factory.create , have been discounted in favor of several others, most notably FactoryGirl.create and the simpler create .

However, experience has shown that some syntaxes do not always match the context.

Take for example:

 FactoryGirl.define do factory :article do after_create {|a| a.comments << create(:comment) } end factory :comment do end end 

If the article has_many Comments and comments belong to the article. In the above factories, a.comments << create(:comment) throws an error Comment(#nnn) expected, got FactoryGirl::Declaration::Static . Change this line to a.comments << FactoryGirl.create(:comment) and the error a.comments << FactoryGirl.create(:comment) away.

It is not clear when one syntax should take precedence over any other form.

+6
source share
2 answers

I realized that shorthand notation is not supported in callbacks (like after_create) with the current version (3.2.0). This information came directly from FactoryGirl teams through Google groups. I will update this question when / if it is added in a future version.

+5
source

According to the FactoryGirl documentation , if you want to omit the prefix of the FactoryGirl module when invoking methods such as creation and assembly in FactoryGirl methods in the rspec / test-unit module as follows:

 # rspec RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end 
+1
source

Source: https://habr.com/ru/post/914387/


All Articles