Factory_bot: Transient vs Ignore

Created on 25 May 2014  路  6Comments  路  Source: thoughtbot/factory_bot

You changed the getting started docs to say to use #transient instead of #ignore in #651, which seems to be a feature coming in 5.0.0, yet the recommended versioning is ~> 4.0. Using transient as in your example

FactoryGirl.define do
  factory :post do
    title "Through the Looking Glass"
    user
  end

  factory :user do
    name "John Doe"

    factory :user_with_posts do
      transient do
        posts_count 5
      end

      after(:create) do |user, evaluator|
        create_list(:post, evaluator.posts_count, user: user)
      end
    end
  end
end

lead to the following errors:
rspec:

it 'returns a list of posts' { create :user_with_posts }
...
Failures:

  1) User#posts returns a list of posts
     Failure/Error: user = FactoryGirl.create(:user_with_posts)
     NoMethodError:
       undefined method `posts_count' for #<FactoryGirl::SyntaxRunner:0x007fb0ffa8cc28>

pry:

FactoryGirl.create(:user_with_posts)
#=> NoMethodError: undefined method `posts_count' for #<FactoryGirl::SyntaxRunner:0x007fa2cb760960>
from /Users/bendyorke/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/evaluator.rb:42:in `method_missing'

Switching it to ignore solved the issue

EDIT: added some markdown

Most helpful comment

@seyonv maybe try:

transient do
    question_count { 5 }
    choice_count { 4 }
  end

All 6 comments

GitHub shows the documentation for the master branch, and not for any released version of the gem. Here's the documentation for the latest release: https://github.com/thoughtbot/factory_girl/blob/v4.4.0/GETTING_STARTED.md#transient-attributes

This problem was confusing me today, until I found out this issue.

It would be good to say in the documentation of master (v 5.0.0), at least for now, that previous versions use ignore. If possible, it would be even better that 5.0.0 supports ignore for backwards compatibility. This will avoid future confusion for people that reads from old documentation.

This has been resolved in 2bf15e4.

I'm using FactoryGirl v 4.5.0 (through factory_girl_rails v4.5) and I have the exact same issue. Replacing transient attribute with ignore attribute does not solve the problem.

factory :survey do
trait(:_1) do
  id 1
  description "SurveyRiskQ"
  transient do
    question_count 5
    choice_count 4
  end
end
trait(:_2) do
  id 2
  description "SurveyHorizonQ"
  transient do
    question_count 3
    choice_count 4
  end
end
factory :survey_with_questions do
  after :create do |survey, evaluator|
    a=create_list(:question,evaluator.question_count)
    survey.questions << a
    end_ind=evaluator.question_count-1
    for i in (0..end_ind)
      sq = survey.survey_questions.where(question: a[i]).first
      sq.weight=5
      choice_list=create_list(:choice,evaluator.choice_count)
      sq.choices << choice_list
      survey.survey_questions << sq
    end
  end
  factory :survey_with_questions_1, traits: [:_1]
  factory :survey_with_questions_2, traits: [:_2]
end

end

For reference, the specific error is

survey_with_questions - undefined method `question_count' for #<FactoryGirl::SyntaxRunner:0x007ff04d969ed0> (NoMethodError)

@seyonv I'm also dealing with the exact same issue. I haven't found a solution yet, but will update if I do.

Edit: I solved mine - I was using posts_count=5 instead of posts_count 5

@seyonv maybe try:

transient do
    question_count { 5 }
    choice_count { 4 }
  end
Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidvgus picture davidvgus  路  5Comments

ge-fa picture ge-fa  路  3Comments

andyw8 picture andyw8  路  4Comments

loybert picture loybert  路  4Comments

chintanparikh picture chintanparikh  路  3Comments