在Rspec中测试STDOUT输出

时间:2013-05-12 11:52:52

标签: ruby rspec rspec2

我正在尝试为此声明构建规范。使用'puts'

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

2 个答案:

答案 0 :(得分:76)

RSpec 3.0 +

RSpec 3.0 added a new output matcher用于此目的:

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

MINITEST

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

答案 1 :(得分: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

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

我也有blog谈论这种事情。