I need to define a factory for a database table which contains column named 'method'.
I've tried something like this:
FactoryGirl.define do
factory :notification_baby_offset, class:'NotificationTemplate' do
...
method {{ 'email' => false, 'notification' => true }}
...
end
end
But, when I tried to build it, I got an error saying:
`method': wrong number of arguments (0 for 1) (ArgumentError)
It appears to identify 'method' as a function rather than a database column. I tried to emphasize this by :method
or "method"
, but that didn't work.
So, I have to build this factory like this for it to work:
FactoryGirl.create(:notification_baby_offset, method: { 'email' => false, 'notification' => true })
Is where a way to avoid this hack and define this column properly in a factory?
Try this:
FactoryGirl.define do
factory :notification_baby_offset, class:'NotificationTemplate' do
...
after(:build) do |notification|
notification.method = { 'email' => false, 'notification' => true }
end
...
end
end
Or even (if your new
supports passing attributes):
FactoryGirl.define do
factory :notification_baby_offset, class:'NotificationTemplate' do
...
initialize_with { new(method: { 'email' => false, 'notification' => true }) }
...
end
end