RSpec消息期望未得到满足

时间:2012-10-25 08:21:06

标签: ruby rspec

我有以下代码:

require_relative '../spec_helper'

describe PaymentProcessor do
  before(:each) do
    @processor = PaymentProcessor.new
  end

  describe '#process' do
    context 'payment accepted, sha digest valid' do
      it 'should return true and have no errors' do
        GamePlayResult.stub(:new).and_return(mock('GamePlayResult'))
        ticket = stub_model(Ticket, player: stub_model(Player, has_funds?: true))
        Ticket.stub(:find).and_return ticket
        game = stub_model(Game, play: ticket, tolerance: 10)
        query = 'orderID=1060&STATUS=5&PAYID=17314217&NCERROR=0&SHASIGN=E969563B64ED6F93F5DC47A86B1B04DFC884B4A7'
        @processor.process(query, game).should_not be_false
        game.should_receive(:play)
        @processor.error.should equal nil
      end
    end
  end
end

正在满足除game.should_receive(:play)以外的所有其他断言。但是我知道:play被调用,因为a)其他断言如果不存在则会失败,b)如果我不存根,我会收到意外的消息错误。

提前致谢。

2 个答案:

答案 0 :(得分:2)

您必须在之前设置RSpec期望,以调用将执行该期望的代码。

所以这个:

@processor.process(query, game).should_not be_false
game.should_receive(:play)

应改为:

game.should_receive(:play)
@processor.process(query, game).should_not be_false

答案 1 :(得分:0)

有点难以看到这里实际调用的是什么代码,但从那里判断我认为它就是这一行:

@processor.process(query, game).should_not be_false

如果是这样,那么问题在于,运行代码之后即将到来,这将无效。将game.should_receive(:play)行移到@processor.process(query, game)行之上,它应该通过:

  it 'should return true and have no errors' do
    GamePlayResult.stub(:new).and_return(mock('GamePlayResult'))
    ticket = stub_model(Ticket, player: stub_model(Player, has_funds?: true))
    Ticket.stub(:find).and_return ticket
    game = stub_model(Game, play: ticket, tolerance: 10)
    query = 'orderID=1060&STATUS=5&PAYID=17314217&NCERROR=0&SHASIGN=E969563B64ED6F93F5DC47A86B1B04DFC884B4A7'
    game.should_receive(:play)
    @processor.process(query, game).should_not be_false
    @processor.error.should equal nil
  end