I've been working with Factory Girl and Spree, and I'm going to share how I solved a problem I ran into by trying to override a factory from Spree, but first, I am going to tell a bit more from what I did.
My test was failing in this factory from Spree Core:
FactoryGirl.define do
factory :credit_card, class: Spree::CreditCard do
verification_value 123
month 12
year { Time.now.year }
number '4111111111111111'
name 'Spree Commerce'
end
end
Did you see the problem? Ding ding ding! Yes, you are right fellow: the year is declared with Time.now.year; but what happens when you are already in that current month? My test was telling me something like:
Credit Card Expired
Here is where I need to do some workarounds to fix this issue. The first thing I tried is to override the FactoryGirl.define:
FactoryGirl.define do
factory :credit_card, class: Spree::CreditCard do
year { Time.now.year + 1 }
end
end
Sadly, this triggered another issue about duplicated declaration, so I went to the FactoryGirl documentation and I found that they have a modify method which I tried to use as follows:
FactoryGirl.modify do
factory :credit_card, class: Spree::CreditCard do
year { Time.now.year + 1 }
end
end
Still no luck. It was not recognizing credit_card as a declared method, so I was not sure at first what was going on. After taking a more detailed look, I made sure I was loading the Spree factories at the beginning of the file:
require 'spree/testing_support/factories'
FactoryGirl.modify do
factory :credit_card, class: Spree::CreditCard do
year { Time.now.year + 1 }
end
end
And after this small change... everything went green!
This workaround helped me a lot, so I hope this can help you as well.
Thanks for reading! See you next time.
Follow me: @zazvick
I also write here: Read more