我有一个Logger类.我想在函数内部测试,Logger将接收log带foo参数的调用.
expect(Logger).to receive(:log).with("foo")
Bar.foo()
Run Code Online (Sandbox Code Playgroud)
然而在此期间一些模型回调还将呼吁Logger.log("euu"),Logger.log("arr"),Logger.log("hmm")和其他许多人.Rspec似乎失败了因为log首先调用了其他一些参数.
我只担心log("foo")被召唤一次.有没有办法做这种测试?
cbl*_*ard 17
方法是允许调用log,执行您的操作,然后检查log已使用预期参数调用的内容,如下所示:
allow(Logger).to receive(:log)
Bar.foo
expect(Logger).to have_received(:log).with("foo")
Run Code Online (Sandbox Code Playgroud)
RSpec将此模式称为间谍
akz*_*z92 11
您可以使用接收计数:
expect(Logger).to receive(:log).with('foo').at_least(:once)
Run Code Online (Sandbox Code Playgroud)