ruby - Rails testing non-AR model(class) with FactoryGirl -
i have class looks this:
class killmail::parser def initialize(body) @body = body end end i have factory it:
factorygirl.define factory :parser, class: killmail::parser skip_create body '2013.12.02 19:24 bla bla' initialize_with { new(attributes) } end end it works fine long don't try change default attributes. when try use this
factorygirl.create(:parser, body: 'some different body') it returns this:
=> #<killmail::parser:0x007fb2ff116548 @body={:body=>"some different body"}> what doing wrong? can't google useful on case.
have tried calling new(body) instead of new(attributes)? problem lie how you're initializing parser class.
attributes creates hash of attributes , passes that...
initialize_with { new(attributes) } # translates parser.new({ body: '2013.12.02 19:24 bla bla' }) while calling #new body passes value first param...
initialize_with { new(body) } # translates parser.new('2013.12.02 19:24 bla bla') and can add many values #new want in same fashion...
# in factorygirl.define new_var_1 'new_string_1' new_var_2 2 initialize_with { new(body, new_var_1, new_var_2) } # translates parser.new('2013.12.02 19:24 bla bla', 'new_string_1', 2)
Comments
Post a Comment