Alex Rivera | Logout

RSpec: how do I write a test that expects certain output but doesn't care about the method?

Asked 2011-06-16T13:36:43.993
14

I'm trying to get my head around test-driven design, specifically RSpec. But I'm having trouble with some of the examples from The RSpec Book.

In the book, we test for output on $STDOUT like this:

output = double('output')
game = Game.new
output.should_receive(:puts).with('Welcome to Codebreaker!')
game.start()

Well, that works after a fashion. But why on earth should I care if the Game object uses the puts() method? If I change it to print(), should it really break the test? And, more importantly, isn't this against the one of the principals of TDD - that I should be testing what the method does (the design) rather than how it does it (the implementation)?

Is there some way I can write a test that just tests what ends up on $STDOUT, without looking at what method puts it there?

Edit
Report

1 Answer

6

take a look at this post. Nick raised questions about the same example, and a very interesting conversation follows in the comments. Hope you find it helpful.

answered 2011-06-17T00:26:18.770

Your Answer