25 Jan 2010, 18:14
Generic-user-small

E Asa Bour (1 post)

I am running into two issues when walking from the Code Breaker example.

1) codebreaker/spec/codebreaker/game_spec.rb:7: undefined method `let’ for Spec::Example::ExampleGroup::Subclass_1::Subclass_1:Class (NoMethodError)

I am using the exact game_spec.rb contained within the provided source code. I am using Ubuntu and have confirmed that the proper gems are installed:

** LOCAL GEMS **

actionmailer (2.3.5) actionpack (2.3.5) activerecord (2.3.5) activeresource (2.3.5) activesupport (2.3.5) builder (2.1.2) cucumber (0.6.2) cucumber-rails (0.2.4) diff-lcs (1.1.2) json_pure (1.2.0) mysql (2.8.1) polyglot (0.2.9) rack (1.1.0) rails (2.3.5) rake (0.8.7) rspec (1.3.0) rspec-rails (1.3.2) selenium-client (1.2.18) term-ansicolor (1.0.4) treetop (1.4.3)

For some reason it appears that Spec::Mocks is not available.

2) undefined method `double’ for main:Object (NoMethodError)

Test Doubles are also not available and this appears to be a related issue.

I even tried to explicitly define rspec.

Spec::Runner.configure do |config| config.mock_with :rspec end

Any help would be greatly appreciated.

Thanks, Asa

26 Jan 2010, 04:37
Dchelimsky_pragsmall

David Chelimsky (206 posts)

What version of Ruby? Also, please post the code in the spec. Hard to tell what’s going wrong without seeing it.

02 Mar 2010, 21:30
Generic-user-small

Jonathan MacDonald (1 post)

Hi Asa,

With respect at least to your first error, I believe you may be trying to use the ‘let’ in the before(:each).

# incorrect
before(:each) do
  let(:output) { double('output').as_null_object }
  let(:game) { Game.new(output) }
end

# correct 
let(:output) { double('output').as_null_object }
let(:game) { Game.new(output) }
30 Mar 2010, 04:00
Pete-lowres-50_pragsmall

Pete Lasko (3 posts)

I’m also on B 13, and have the same error. However, I’m on Ruby 1.8.7 and Rspec 2. I’ll move on if I can’t get this to work, but I’d like to try and understand what is happening.

#game_spec.rb

require 'spec_helper'

module Codebreaker
  describe Game do
    describe "#start" do
      it "sends a welcome message" do
        output = double('output')
        game = Game.new(output)

        output.should_receive(:puts).with('Welcome to Codebreaker!')

        game.start
      end

      it "prompts for the first guess"
    end
  end
end
1) Codebreaker::Game #start sends a welcome message
    Failure/Error: output = double('output')
    undefined method `double' for #<Rspec::Core::ExampleGroup::NestedLevel_1::NestedLevel_1:0x1016f64e8>
    # ./spec/codebreaker/game_spec.rb:15
31 Mar 2010, 12:40
Dchelimsky_pragsmall

David Chelimsky (206 posts)

Pete - the error message tells you everything you need to know: “undefined method `double’”.

The “double” method was added to rspec-1 but was not forward-ported, yet, to rspec-2. It’s in the rspec-2 codebase now, so it will be part of 2.0.0.beta.5, coming soon.

Also, the book has not been updated for rspec-2 yet, so you’re probably best off sticking with rspec-1 until it is.

06 Apr 2010, 01:44
Pete-lowres-50_pragsmall

Pete Lasko (3 posts)

Thanks for the heads up. I’d guessed that it was probably missing in the rspec2 codebase. Excited to see it working soon. I’ll figure out how to get back to version 1 for now. Thanks.

21 Dec 2010, 13:48
Generic-user-small

George L. Githinji (5 posts)

Has this method been ported for Rspec-2 now? I am running on the same issue.

21 Dec 2010, 19:00
Dchelimsky_pragsmall

David Chelimsky (206 posts)

Yes, George. This thread is from 8 months ago, long before RSpec-2 and the book were released.

23 Dec 2010, 00:12
Vi_pragsmall

Victor Daniel Guiomar Martins (2 posts)

I’m having trouble has well, the as_null_object doesn’t seem to work has expected. My first expectation is green but the second is red, meaning that only the first output is getting to be null?? I’m on page 71/72, before the Refactor.

The code is this:

      it 'sends a welcome message' do
        #given(this and this)
        output = double('output').as_null_object
        game = Game.new(output)
        #expectation(I expect this to happen)
        output.should_receive(:puts).with('Welcome Codebreaker!')
        game.start
      end

      it 'prompts for the first guess' do
        output = double('output').as_null_object
        game = Game.new(output)
        output.should_receive(:puts).with('Enter guess')
        game.start
      end

And the error for the “prompts for the first guess” is:

RSpec::Mocks::MockExpectationError: Double “output” received :puts with unexpected arguments expected: (“Enter guess”) got: (“Welcome Codebreaker!”), (“Make a guess”)

Running Rspec 2.3.0 and Ruby 1.9.2

23 Dec 2010, 01:12
Dchelimsky_pragsmall

David Chelimsky (206 posts)

Victor - as_null_object is working fine. Look at the failure message!

23 Dec 2010, 10:14
Vi_pragsmall

Victor Daniel Guiomar Martins (2 posts)

Dumb me, sorry David! I was coding late at night and never spotted the “Enter/Make a”. And I had the idea that the as_null_object would make the output to have just one string and not two. I have to read about that pattern.

Sorry again, and thank for the awesome work!

21 Sep 2012, 12:25
John_pragsmall

m.v john (1 post)

Thanks Jonathan MacDonald. you answer solved my problem

John

  You must be logged in to comment