Reputation: 26031
Question: How can i use for one field both sequence and transient attribute?
Background: I have factory, which has a name. The name is sequence to keep it unique. However in few specs i need it set name chosen by me so i can predict it in expectation. It's not a Rails project.
In my head it looks like name {attribute_from_create_call||FactoryGirl.generate :name}
. But i don't know how to get the attribute which i give to the create method
Factory:
FactoryGirl.define do
sequence :name do |n|
'Testing Bridge '+n.to_s
end
factory :historical_bridge do
name {FactoryGirl.generate :name}
end
end
Usage of factory: FactoryGirl.create :historical_bridge, name: 'Bridge from '+Time.now.to_s
Upvotes: 0
Views: 77
Reputation: 373
I think you should not use predefined name instead of sequence but check something like:
let(:bridge) { create :historical_bridge }
it { HistoricalBridge.something.name.should == bridge.name }
Upvotes: 0
Reputation: 1834
You can use FactoryGirl to create a hash of attributes with the sequences and then merge in whatever changes to that hash you want:
new_name = 'Bridge from '+Time.now.to_s
attr = FactoryGirl.attributes_for(:historical_bridge).merge(:name => new_name)
And then you could do something perhaps like create an object with those custom attributes:
HistoricalBridge.create(attr)
Upvotes: 1