rspec2 - how to pass block to RSpec "with" expectation -
tet have class
class foo < activerecord::base def set_publication publication.new |publication| publication.owner_type = 'foo' publication.owner_id = 123 end return 'something else' end end question: how can test block publication new instance receive
describe foo, 'set_publication' let(:foo){ foo.new } publication.should_recive(:new).with( ??????? ).and_return( double(:something) ) foo.set_publication end end of course example of complicated functionality in cannot use hash arguments this
class foo < activerecord::base def set_publication publication.new owner_type: 'foo', owner_id: 123 return 'something else' end end and test this
describe foo, 'set_publication' let(:foo){ foo.new } publication.should_recive(:new).with( owner_type: "foo", owner_id: 123 ).and_return( double(:something) ) foo.set_publication end end thank you
update: seems it's bit unclear i'm asking here, :
i'm looking way ensure publication.new called explicitly set of arguments, in case block
so suppose
publication.should_receive(:new).with(&block) # example where block parameters owner_type == 'foo' , owner_id == 123
you can use and_yield accomplish this.
class foo def set_publication publication.new |publication| publication.owner_type = 'foo' publication.owner_id = 123 end return 'something else' end end class publication attr_accessor :owner_id, :owner_type def initialize yield self if block_given? end end and spec
describe foo let(:foo) { foo.new } let!(:publication) { publication.new } publication.should_receive(:new).and_yield(publication) foo.set_publication publication.owner_type.should eq 'foo' publication.owner_id.should eq 123 end end
Comments
Post a Comment