春節說我有類如何阻止傳遞到RSpec的「與」預期
Class Foo < ActiveRecord::Base
def set_publication
Publication.new do |publication|
publication.owner_type = 'Foo'
publication.owner_id = 123
end
return 'something else'
end
end
問:我如何測試塊出版新的實例將收到
describe Foo, 'set_publication' do
let(:foo){ Foo.new }
it do
Publication.should_recive(:new).with(???????).and_return(double(:something))
foo.set_publication
end
end
當然
這只是一個非常複雜的功能的例子,我不能使用像這樣的散列參數
Class Foo < ActiveRecord::Base
def set_publication
Publication.new owner_type: 'Foo', owner_id: 123
return 'something else'
end
end
並測試它像這樣
describe Foo, 'set_publication' do
let(:foo){ Foo.new }
it do
Publication.should_recive(:new).with(owner_type: "Foo", owner_id: 123).and_return(double(:something))
foo.set_publication
end
end
謝謝
UPDATE:這似乎是有點不清楚我問這裏有什麼,所以:
我在尋找確保Publication.new
被顯式調用一組參數,在這種情況下,一個塊
so我想這樣的事情
Publication.should_receive(:new).with(&block) # example
其中塊參數owner_type == '富' 和owner_id == 123
非常優雅,thx – equivalent8