运行调用ping,通常会设置network_status = true。然后它应该调用connect,如果它是假的:
def run
ping
if @network_status == false
connect
end
end
我想测试它,我写了这个:
t = Test.new
#other tests happen inside the run method, then
it "calls .connect" do
t.stub(:network_status).and_return(false)
t.stub(:ping).and_return(false)
expect(t).to receive(:connect)
t.run
end
但结果是:
Failure/Error: expect(Test).to receive(:connect)
(<Test (class)>).connect(any args)
expected: 1 time with any arguments
received: 0 times with any arguments
为什么?
答案 0 :(得分:2)
@network_status
不是一种方法。这是一个即时变量。您应该在测试中设置即时变量。
it "calls .connect" do
t.instance_variable_set(:@network_status, false)
t.stub(:ping).and_return(false)
expect(t).to receive(:connect)
t.run
end