Question

Both examples are going to STDOUT, but cucumber only sees the first one. The second scenario fails with:

Then the stdout should contain "test"  # aruba-0.4.11/lib/aruba/cucumber.rb:82
  expected "" to include "test" (RSpec::Expectations::ExpectationNotMetError)
  features/test.feature:13:in `Then the output should contain "test"'

The features:

Scenario: echo test
  Given a blank slate
  When I run `echo "test"`
  The stdout should contain "test"

Scenario: puts test
  Given a blank slate
  When I start the program
  The stdout should contain "test"

The step definitions:

When /^I start the program$/ do
  TestModule::Main.new.start
end

The code:

module TestModule
  class Main
    def initialize
    end
    def start
      $stdout.puts "test"
    end
  end
end
Was it helpful?

Solution

I'm not that familiar with Aruba, but a quick peek into it's source code suggests that the assertions it makes against STDOUT (or any output) only apply to processes that it started itself, and not all content that's been written to STDOUT. The code that you invoke yourself, in the second scenario, is outside of the control of Aruba, so it's output won't be tracked.

If you think about it, it couldn't really work any other way - if Aruba captured all STDOUT for assertions, then it would contain Cucumber's own test output as well...

It looks like you're trying to test your program in-process without using Aruba to invoke a separate Ruby process. If that's the case I'd suggest modifying the program to make it possible to pass in a STDOUT replacement e.g.

def initialize(output=$stdout)

Then when you start the code:

When /^I start the program$/ do
    TestModule::Main.new(@output).start
end

And you can change your assertion:

Then the stdout should contain "(.+)" do |string|
    @output.should include string
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top