Rspec with Ruby basic output String testing raises an error -
i've tried write basic test rspec, in order test reception of string on standard output.
i've stubed standard output same way it's written in rspec book :
require './tweetag.rb' module tweetag describe tweet describe "#print" "prints test" output = double('output').as_null_object t = tweetag::tweet.new(output) t.print output.should_receive(:puts).with('test') end end end end
the ruby code looks :
module tweetag class tweet def initialize(output) @output=output end def print @output.puts('test') end end end
as can see, nothing complicated. still, answer receive after running specs following :
failures: 1) tweetag::tweet#print prints test failure/error: output.should_receive(:puts).with('test') (double "output").puts("test") expected: 1 time received: 0 times
i've tried removing "as_null_object" , answer :
1) tweetag::tweet#print prints test failure/error: t.print double "output" received unexpected message :puts ("test")
thank help.
the should_receive
method has used before calling method.
output = double('output').as_null_object t = tweetag::tweet.new(output) output.should_receive(:puts).with('test') t.print
as side note, test missing check on return value here. know print method doesn't raises exception. don't check return value appropriate.
output.should_receive(:puts).with('test').and_return('returned value') t.print.should eql('returned value')
Comments
Post a Comment