user1376243
user1376243

Reputation: 307

Rspec Cucumber: NoMethodError

I'm followiing 'The Rspec Book' and I can't comprehend why I'm getting the following error when running cucumber.

Feature: code-breaker starts game

  As a code-breaker
  I want to start a game
  So that I can break the code

  Scenario: start game                          # /Users/PC/ruby/practice/rspec/codebreaker/features/codebreaker_starts_game.feature:7
    Given I am not yet playing                  # ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:17
    When I start a new game                     # ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:20
    Then I should see "Welcome to Codebreaker!" # ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:25
      undefined method `messages' for #<RSpec::Matchers::BuiltIn::Output:0x007fd6611fcb30> (NoMethodError)
      ./ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:26:in `/^I should see "(.*?)"$/'
      ./ruby/practice/rspec/codebreaker/features/codebreaker_starts_game.feature:10:in `Then I should see "Welcome to Codebreaker!"'
    And I should see "Enter guess:"             # ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:25

Failing Scenarios:
cucumber /Users/PC/ruby/practice/rspec/codebreaker/features/codebreaker_starts_game.feature:7 # Scenario: start game

1 scenario (1 failed)
4 steps (1 failed, 1 skipped, 2 passed)
0m0.050s

shell returned 1

step definitions file:

http://pastebin.com/BZZKL0wa

Note: I tried printing output.messages and it worked fine.

Upvotes: 1

Views: 218

Answers (2)

user3887150
user3887150

Reputation:

Peter Alfvin is right: rename the output method. Here's what worked for me:

class OutputDouble
  def messages
    @messages ||= []
  end
  def puts(message)
    messages << message
  end
end

def output_double
  @output ||= OutputDouble.new
end

Given /^I am not yet playing$/ do
end

When /^I start a new game$/ do
  game = Codebreaker::Game.new(output_double)
  game.start
end

Then /^I should see "([^"]*)"$/ do |message|
  output_double.messages.should include(message)
end

Note that the creation method (output) has been renamed to output_double.

Upvotes: 2

Peter Alfvin
Peter Alfvin

Reputation: 29389

I believe that you're running into the built in output matcher that's part of RSpec (see https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/output-matcher). Did you try printing output.messages within your step definition at the same point you're trying to check the output? You should get the same failure.

In any event, if you use a different method name, you should be ok.

Upvotes: 3

Related Questions