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

时间:2015-03-10 16:55:45

标签: ruby rspec rspec3

这两个测试有何不同,一个是通过,另一个是失败?我怎么让他们都通过?

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

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

spec文件的适用子集:

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?`.

1 个答案:

答案 0 :(得分:3)

你有一个错字,改变:

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

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