如何使用RSpec
来验证方法是否接收到特定块?考虑这个简化的例子:
class MyTest
def self.apply_all_blocks(collection, target)
collection.blocks.each do |block|
target.use_block(&block)
end
end
end
我想要一个规范来验证target.use_block
每个由collection.blocks
.
以下代码不起作用:
describe "MyTest" do
describe ".apply_all_blocks" do
it "applies each block in the collection" do
target = double(Object)
target.stub(:use_block)
collection = double(Object)
collection.stub(:blocks).and_return([:a, :b, :c])
target.should_receive(:use_block).with(:a)
target.should_receive(:use_block).with(:b)
target.should_receive(:use_block).with(:c)
MyTest.apply_all_blocks(collection, target)
end
end
end
(另外,use_block
不一定调用块,因此测试块接收到是不够的call
。同样,我不认为target.should_receive(:use_block).and_yield
会做我想做的事。)