我现在正在使用test /Smashing构建一个应用程序,并且我正在使用rspec来测试我的代码。但是,我不知道如何检查send_event
是否被调用。我试过了
expect(Sinatra::Application).to receive(:send_event).twice
和
expect(Dashing).to receive(:send_event).twice
,
但这两种方法都没有奏效。我不确定哪个对象应该接收对send_event
的调用,因为它位于app.rb
中的Dashing中。在时髦的GitHub上,也有this issue在谈论同样的事情,但没有得到回复。
任何关于如何做到这一点的建议都将不胜感激。谢谢!
更新:
我仍然不知道如何做到这一点,但我发现这是有效的:
let(:dummy_class) { Class.new { include Dashing } }
context 'something' do
it 'does something' do
expect(dummy_class).to receive(:send_event).once
dummy_class.send('send_event', 'test', current: 'test')
end
end
但是,如果我使用要调用的包含send_event
的方法,而不是使用dummy_class.send(...)
,那么它无法识别该方法已被调用。这一定与我的测试没有使用虚拟类有关。我不知道是否有任何方法可以绕过这个问题,让它使用虚拟类。
https://stackoverflow.com/questions/50766147
复制相似问题