47

我正在尝试为此声明构建规范。'puts'很容易

print "'#{@file}' doesn't exist: Create Empty File (y/n)?"
4

2 回答 2

95

RSpec 3.0+

RSpec 3.0为此添加了一个新的output匹配器:

expect { my_method }.to output("my message").to_stdout
expect { my_method }.to output("my error").to_stderr

迷你测试

Minitest 还有一个叫做capture_io

out, err = capture_io do
  my_method
end

assert_equals "my message", out
assert_equals "my error", err

RSpec < 3.0(和其他)

对于 RSpec < 3.0 和其他框架,您可以使用以下帮助程序。这将允许您分别捕获发送到 stdout 和 stderr 的任何内容:

require 'stringio'

def capture_stdout(&blk)
  old = $stdout
  $stdout = fake = StringIO.new
  blk.call
  fake.string
ensure
  $stdout = old
end

def capture_stderr(&blk)
  old = $stderr
  $stderr = fake = StringIO.new
  blk.call
  fake.string
ensure
  $stderr = old
end

现在,当你有一个方法应该在控制台上打印一些东西时

def my_method
  # ...
  print "my message"
end

你可以写一个这样的规范:

it 'should print "my message"' do
  printed = capture_stdout do
    my_method # do your actual method call
  end

  printed.should eq("my message")
end
于 2013-05-12T13:06:44.017 回答
3

如果您的目标只是能够测试此方法,我会这样做:

class Executable
  def initialize(outstream, instream, file)
    @outstream, @instream, @file = outstream, instream, file
  end

  def prompt_create_file
    @outstream.print "'#{@file}' doesn't exist: Create Empty File (y/n)?"
  end
end


# when executing for real, you would do something like
# Executable.new $stdout, $stdin, ARGV[0]

# when testing, you would do
describe 'Executable' do
  before { @input = '' }
  let(:instream)   { StringIO.new @input }
  let(:outstream)  { StringIO.new }
  let(:filename)   { File.expand_path '../testfile', __FILE__ }
  let(:executable) { Executable.new outstream, instream, filename }

  specify 'prompt_create_file prompts the user to create a new file' do
    executable.prompt_create_file
    outstream.string.should include "Create Empty File (y/n)"
  end
end

但是,我想指出,我不会直接测试这样的方法。相反,我会测试使用它的代码。昨天我正在和一个潜在的学徒交谈,他正在做一些非常相似的事情,所以我和他坐下来,我们重新实现了一部分课程,你可以在这里看到。

我也有一个博客谈论这种事情。

于 2013-05-12T13:21:44.193 回答