1

我有以下代码:

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)如果我不存根它,我会收到一个意外的消息错误。

提前致谢。

4

2 回答 2

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
于 2012-10-25T08:30:30.407 回答
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
于 2012-10-25T08:30:10.090 回答