small medium large xlarge

07 Oct 2012, 07:13
Iain Wood (6 posts)

Working through chapter 8 I had some trouble using an after hook. If I give it a puts statement I see no output on the terminal window. Before works fine, I can see my “Starting scenario” string in the output, but not my “finished scenario” string.

To isolate any potential typos, I started again with a direct copy of the source provided in code/support_code/18 and made 2 small changes.

1) alter the feature so it fails 2) change the line in debugging.rb from: save_and_open_page if scenario.failed? to: puts “finished scenario”

Although the after statement is being run (I know this because save_and_open_page ran before I made the change) I never see the string being output. Any idea why? My setup is fairly standard. I’m on a Mac running ruby 1.9.3-p194 and have tried running cucumber via bundle exec so I should be getting all the right gem versions.

Regards Iain

08 Oct 2012, 06:19
Matt Wynne (91 posts)

Hi Ian,

I think you’ve found a bug!

Cucumber intercepts calls to @#puts@ so that it send the text to the formatter for outputting. That’s why you see the before hook’s output in blue.

It looks as though the formatter is printing before the after hook as been run, so it’s too late to show any output from the hook. I’m surprised nobody has spotted this before. Thanks for pointing this out!

If you want to see some output, use @$stdout.puts@ and that will write directly to the stream, bypassing Cucumber’s interception.

08 Oct 2012, 07:29
Iain Wood (6 posts)

Ah, thanks for clarifying that.

In the process of trying to understand what was going on I tried using an Around hook instead. Around runs fine, and displays before and after puts messages (in grey not blue) correctly. However I hit another problem. It doesn’t seem to understand the context in the same way. I get this error:

undefined local variable or method `save_and_open_page’ for nil:NilClass ( NameError)

Any idea what I need to add, and more interestingly, why does Around behave differently from Before and After?


08 Oct 2012, 10:15
Matt Wynne (91 posts)

This is a known issue. See for details.