Reputation: 49
I have a model where multiple params are passed to the initialize during object instantiation. Is there a way how i can skip the method from getting executed at all in factorygirl.
My model file is shown below:
class WebResponseCache < ActiveRecord::Base
validates_presence_of :q, :results
self.table_name = "web_response_cache"
def initialize(q, results)
super()
self.q = q
self.results = results
end
My factory girl code is shown below:
FactoryGirl.define do
factory :google_web_response_cache, :class => WebResponseCache do
initialize_with { new('query', 'results') }
end
end
Upvotes: 0
Views: 348
Reputation: 7044
You can't skip this method from getting executed, as it is the constructor. But you can change it in order to make the parameters unnecessary.
def initialize(attrs = nil)
super()
return unless attrs
self.q = attrs[:q]
self.results = attrs[:results]
end
Now you can do
WebResponseCache.new # no params passed
WebResponseCache.new(q: 'query', results: 'results') # will be initialized with given params
And now you don't need initialize_with
block in your factory.
Upvotes: 3