Richbits
Richbits

Reputation: 7624

Factory Girl Using Multiple Override properties

I have a simple factory currently defined as:

factory :tm_event do
    ...
    category "MyString"
    category_id { Random.rand(1..2147483647) }
    ...
end

I attempting to create a TmEvent object with the following:

FactoryGirl.create(:tm_event, category: 'rock', category_id: '12')

When this is run both category and category_id are set to nil within the resultant object.

If I run with either category or category_id set individually then both instances are set with the corresponding property overridden. When I create an event directly TmEvent.create(category: 'rock', category_id:'12') then both attributes are populated.

Is my syntax correct? Where should I look for a solution?

Upvotes: 1

Views: 306

Answers (1)

Raj
Raj

Reputation: 22946

Your syntax seems right. I just tried out the same in my console. It updates both the attributes.

2.1.2 :010 >   FactoryGirl.build(:user)
 => #<User id: nil, email: "[email protected]", encrypted_password: "$2a$10$WnXKcpKZt0kkkXyRRe/QP.NS7mIaFcgFzCpZhW0hzEO...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, god: nil, name: "Test User 123", sash_id: nil, level: 0, confirmation_token: nil, confirmed_at: "2014-09-12 11:58:28", confirmation_sent_at: nil, unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil>
2.1.2 :011 > FactoryGirl.build(:user, email: '123', sign_in_count: 2)
 => #<User id: nil, email: "123", encrypted_password: "$2a$10$JIjhQp40Lz/2fCskW63lzOHHxV2NwX2h5URswbuZsgH...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, god: nil, name: "Test User 123", sash_id: nil, level: 0, confirmation_token: nil, confirmed_at: "2014-09-12 11:58:28", confirmation_sent_at: nil, unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil>
2.1.2 :012 >

Try out in your console.

Upvotes: 1

Related Questions