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 に答える