ruby on rails - How do I test with Rspec an initialize method that invokes find_or_initialize_by? -
how write test check find_or_initialize_by
block in initialize
method?
def initialize(document, user) @document = document @api = @document.api_version @template = @document.template @user = user @brand = @user.brand @vars = master_var_hash.extend hashie::extensions::deepfind template_variables.each |t| @document.template_variables.find_or_initialize_by(name: t.name) |d| d.name = t.name d.tag = t.tag d.box_name = t.box_name d.designator = t.designator d.order_index = t.order_index d.master_id = t.id d.editable = t.editable d.editable_title = t.editable_title d.html_box = t.box.stack.html_box if @api == :v3 d.text = t.name == 'title' ? default_title : user_value(t) end end end
i want able test right values have been assigned @document
's templatevariables
class' templatevariable
s. in coverage report can't hit inside find_or_initialize_by
block.
my test size doesn't check want test here:
describe 'template_variables' 'initializes new vars per document' expect(document.template_variables.size).to eq subject.master_var_hash.size end end
how can write test check values , cover lines?
you use
expect(document).to receive_message_chain(:template_variables, :find_or_initialize_by).exactly(8).times
but it's mess , have check if each call got proper parameters.
i suggest extracting method: document#initialize_variables(template_variables)
then test as
expect(document).to receive(:initialize_variables).with(expected_hash)
and can cover document#initialize_variables
specs , test it's behavior in depth.
Comments
Post a Comment