私はRubyの単体テストには新しく、STDOUTをチェックする仕様を作成するのにいくつか問題があります。私はテストしているコードがクラス/メソッド内にある場合にSTDOUTをテストする方法を知っていますが、クラスまたはメソッド内にない通常のRubyファイルから来るputs
ステートメントをテストしたいだけです。RSpecのSTDOUTの取得
Failures:
1) Something I'm Testing should print Welcome to the Book Club!
Failure/Error: expect {$stdout}.to output("Welcome to the Book Club!\n").to_stdout
expected block to output "Welcome to the Book Club!\n" to stdout, but output nothing
Diff:
@@ -1,2 +1 @@
-Welcome to the Book Club!
# ./spec/my_spec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.01663 seconds (files took 0.08195 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/my_spec.rb:7 # Something I'm Testing should print Welcome to the Book Club!
:ここ
はmy_file.rb
puts "Welcome to the Book Club!"
から簡単な1つのライナーはここで私は、次のエラーメッセージが表示されます仕様
my_spec.rb
require_relative 'my_file.rb'
describe "Something I'm Testing" do
it 'should print Welcome to the Book Club!' do
expect {$stdout}.to output("Welcome to the Book Club!\n").to_stdout
end
end
だです
を使用してmy_file.rb
からSTDOUTをキャプチャすることはできますが、それは機能しません。出力は何も言わないままです。私は出力をキャプチャすることについて話すこの記事を見つけましたStringIO
Testing STDOUT output in Rspec
しかし、その方法は私のためには機能しませんでした。私は間違って何をしていますか?
ありがとうございます!
ありがとう!魅力のように働いた。次回は心に留めておきます。 – SalceCodec
@Uzbekjonがあなたの質問に答えた場合は、チェックアイコンをクリックして回答を受け入れてください。詳細については、http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-workを参照してください。 –