Is there a way in factory_girl to get the_for attributes and create for the same instance element?

If I want to create and use an instance using the create build strategy, and then you want to use the attributes_for build strategy to check, can this be done? And if I use sequences in a factory? Is this possible in machinist stone?

+5
source share
3 answers

Not quite sure what I fully understand. And I am not a typist user. But it looks 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]
+11
source

Thanks for this post, I just wanted to add that the FactoryGirl class

@user_attributes = FactoryGirl.attributes_for(:super_user)
+1

Solution John Hinnegan offers sound, but it is better to use the method FactoryGirl.createto initialize the object, since it usually gives you a valid object. For example, after(:create)it will not 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]
+1
source

All Articles