假设我正在测试以下类:
class Processor
def initialize(tree)
@tree = tree
end
def process(entity)
@tree.each_branch do |branch|
branch.inject({}) do |result, fruit|
result[fruit.name] = fruit.type == entity.type
end
end
end
end
我想注入一个存根树,在我的规范中我会:
describe Processor do
let(:tree) { double("tree") }
let(:apple) { Fruit.new("apple") }
let(:processor) { Processor.new(tree) }
let(:fruit1) { Fruit.new("orange") }
let(:fruit2) { Fruit.new("apple") }
it "should process a fruit"
tree.stub(:each_branch).and_yield([fruit1, fruit2])
Processor.process(apple)
end
end
我希望在块中创建以下哈希。如何验证它是否正确创建并返回给块的调用者?
{ "orange" => false, "apple" => true }
编辑:我省略了 Fruit 类的细节,它应该是无关紧要的。