I'm new to Ruby.
Today I made a rake task that just outputs "Hello World".
# rake hoge:hello
"Hello World"
I wish I had written a test to make sure that the result contained "Hello" and could find it simply by specifying a string. .. ..
hoge_spec.rb
require 'rails_helper'
require 'rake'
RSpec.describe 'Hoge', type: :task do
#Omission
describe 'rake hoge:hello' do
let(:task) { 'hoge:hello' }
context 'Find the standard output Hello' do
it 'Hello on standard output#1 Character string as an argument of output matcher' do
#* Ideal type. However, it fails because it does not match exactly. .. ..
expect{ @rake[task].invoke() }.to output('Hello').to_stdout
end
it 'Hello on standard output#2 Regular expression in the argument of output matcher' do
#* It works, but regular expression matches are difficult to use. .. ..
expect{ @rake[task].invoke() }.to output(/Hello/).to_stdout
end
it 'Hello on standard output#Partial match with 3 include' do
#* It works, but the code is long. .. ..
$stdout = StringIO.new
@rake[task].invoke()
output_text = $stdout.string
$stdout = STDOUT
expect(output_text).to include 'Hello'
end
end
end
end
... I thought it would be nice if I could specify a character string (# 1), but an error. The only way to achieve the purpose is to use a regular expression (# 2) or assign the standard output to a variable (# 3). .. .. I'm wondering if there is any good way.
Recommended Posts