I have a simple :item
factory with a nested attachment that is working fine.
FactoryGirl.define do
factory :item do
before_create do |item|
item.attachments << FactoryGirl.build(:attachment, attachable: item)
end
end
end
I want to check for the following
it "is invalid without a photo" do
FactoryGirl.create(:item).should_not be_valid
end
How can I delete the attachments when calling the existing :item
factory?
You may want to have two versions of the item factory. One that does not create the associated attachments, and one that does. This will let you have an item factory for other places without the dependency on attachments.
FactoryGirl.define do
factory :item
end
FactoryGirl.define do
factory :item_with_attachments, :parent => :item do
before_create do |item|
item.attachments << FactoryGirl.build(:attachment, attachable: item)
end
end
end
Another option would be to just remove the attachments before testing its validity:
it "is invalid without a photo" do
item = FactoryGirl.create(:item)
item.attachments.destroy_all
item.should_not be_valid
end