If I want to create and instance using "create" build strategy and then want to use "attributes_for" build strategy for verification, is it possible to do? And if I use sequences in the factory? Is it possible in Machinist gem?
Asked
Active
Viewed 5,089 times
3 Answers
12
Not quite sure I fully understand. And I'm not a user of machinist. But it sounds like you just want to do something like this.
@attributes = FactoryGirl.attributes_for(:my_object)
my_object = MyObject.create(@attributes)
my_object.some_property.should == @attributes[:some_property]

wintersolutions
- 5,173
- 5
- 30
- 51

John Hinnegan
- 5,864
- 2
- 48
- 64
2
The solution John Hinnegan suggest is sound, but you better use the FactoryGirl.create
method for object initialization, because it will usually give you a valid Object. For example a after(:create)
won't be called if you use MyObject.new
.
@attributes = FactoryGirl.attributes_for(:my_object)
my_object = FactoryGirl.create(:my_object, @attributes)
expect(my_object.some_property).to eq @attributes[:some_property]

Community
- 1
- 1

wintersolutions
- 5,173
- 5
- 30
- 51
1
Thanks for this post, just wanted to add that the class is FactoryGirl
@user_attributes = FactoryGirl.attributes_for(:super_user)

nowRails
- 267
- 2
- 5