两个几乎相同的规格;一个通过,一个失败

Two nearly identical specs; one passes, one fails

这两项测试有何不同,一项通过,一项未通过?我如何让他们都通过?

正在测试的文件的适用子集:

class Interface
#Takes the place of the standard I/O to enable testing

  def initialize(session, out = $STDOUT)
    #ties to session
    @session = session
    @out = out
  end

  def get_player_name(mark)
    @out.puts "Player #{mark} name:"
    gets.chomp
  end

  def get_coin_call(caller)
    @out.puts "\n#{caller.name}, heads or tails?"
    gets.chomp.upcase
  end
end

规范文件的适用子集:

describe Interface do
  let (:fake_stdout)  {double("STDOUT")}
  let (:player1) {instance_double("Player", :name => "Bob")}
  let (:session) {instance_double("Session")}
  let (:interface) { Interface.new(session, fake_stdout) }

describe "#get_player_name" do
  it "prompts players to enter their names" do
    expect(fake_stdout).to receive(:puts).with("Player X name:")
    interface.get_player_name("X")
  end
end

describe "#get_coin_call" do
  it "prompts players to answer HEADS or TAILS" do
    expect{fake_stdout}.to receive(:puts).with("\nBob, heads or tails?")
    interface.get_coin_call(player1)
  end
end

第一个测试通过,第二个测试抛出如下错误信息:

 Failure/Error: expect{fake_stdout}.to receive(:puts).with("\nBob, heads or tails?")
   You must pass an argument rather than a block to use the provided matcher (#<RSpec::Mocks::Matchers::Receive:0x007fa8b8e9b340>), or the matcher must implement `supports_block_expectations?`.

你打错了,改:

expect{fake_stdout}.to receive(:puts).with("\nBob, heads or tails?")

expect(fake_stdout).to receive(:puts).with("\nBob, heads or tails?")