1
我想检查块是否在我的函数中使用rspec调用。下面是我的代码:如何检查块使用rspec调用
class SP
def speak(options={},&block)
puts "speak called"
block.call()
rescue ZeroDivisionError => e
end
end
describe SP do
it "testing speak functionality can receive a block" do
sp = SP.new
def test_func
a = 1
end
sp_mock = double(sp)
expect(sp_mock).to receive(:speak).with(test_func)
sp.speak(test_func)
end
end
下面是我的错误:
SP testing speak functionality can receive a block
Failure/Error: block.call()
NoMethodError:
undefined method `call' for nil:NilClass
# ./test.rb:9:in `speak'
# ./test.rb:25:in `block (2 levels) in <top (required)>'
能否请你帮忙。我花了很多时间。
这就是'yield_control'的作用,不是吗? – Stefan
@Stefan这取决于你真正想测试的东西。在这种情况下'.yield_with_no_args'将是最具体的测试。但是由于'SP#speak'的行为取决于传递的块,因此您应该也覆盖这些情况。在测试设置中,传递的块应该“引发ZeroDivisionError”。 –