未满足RSpec消息期望,但调用了方法
我有以下测试:未满足RSpec消息期望,但调用了方法,rspec,mocking,Rspec,Mocking,我有以下测试: describe "Exporter#get_method" do before(:each) do exporter.should_receive(:get_method).at_least(:once).and_call_original end it "should get the next terminal value" do exporter.send(:get_method).should == :split
describe "Exporter#get_method" do
before(:each) do
exporter.should_receive(:get_method).at_least(:once).and_call_original
end
it "should get the next terminal value" do
exporter.send(:get_method).should == :split
end
it "should call descend if the current value is a hash" do
exporter.should_receive(:descend).once
2.times { exporter.send(:get_method) }
end
it "should call ascend if at the end of an array and there is a prologue" do
exporter.should_receive(:ascend).once
3.times { exporter.send(:get_method) }
end
end
我可以通过几个binding.pry调用来验证是否正在调用上升和下降。然而,RSpec没有看到这一点。我哪里做错了。我想确保被测试的方法在正确的情况下调用其他方法。还有别的方法吗?我倾向于从不把期望放在
块之前。在我看来,before
块实际上只是用于设置您正在测试的情况,而不是消除期望。当您移动导出器时会发生什么情况。您是否应该接收(:get\u方法)。至少(:一次)。并且从之前的块中调用\u original
,并将其分发到3个it
块中(根据需要在三种情况中的每种情况下对其进行编辑)?它可能与其他应接收的呼叫冲突
另外,如果您调用的是exporter.get\u方法,而不是exporter.send(:get\u方法)
,它会工作吗?一般来说,RSpec旨在测试行为,而不是实现,如果get\u方法
是私有方法,那么直接测试它就没有任何意义。相反,您可能想做的是为使用该私有方法的方法编写测试。否则,如果它不是私有方法,为什么要使用:send
?无骰子。在特定的it块中,调用_original也会遇到同样的失败。这是一种私有方法。然而,我想通过测试,这样我就知道如果我在未来打破它。要么通过改变它做出决定的条件,要么通过修改方法而没有意识到我也修改了行为。文件中是否有其他存根(可能位于顶部)?这是一个经常让你陷入困境的陷阱……其实不是,只有两个let语句:let(:exporter){Export::exporter.new'data',[:split,{:to_=>[:split]}let(:formats){{{:xls=>Export::Formatters::XLSFormatter,:csv=>Export::Formatters::CSVFormatter}确保规范中发送的应接收
消息的Export::Exporter
实例与接收上升
或下降
的实例相同。有可能是同一类的不同实例。将“应该接收”从“之前”移到“测试”中本应解决这一问题,但仍然失败。