Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:23:40+00:00 2026-06-12T12:23:40+00:00

While working my way through The RSpec Book, I came across the as_null_object method.

  • 0

While working my way through The RSpec Book, I came across the as_null_object method. I don’t understand the explanation the book provides as to why it’s needed:

… the simplest way is to tell the double output to only listen for the messages we tell it to expect and ignore any other messages.

But why does the example code fail? When we call double('output') in each example, are we not creating a new double object for each example and sending it a single message?

What I would like is an deeper explanation (than the book) of why the example code fails, and how as_null_object addresses the issue.

  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" do
    output = double('output')
    game = Game.new(output)
    output.should_receive(:puts).with('Enter guess:')
    game.start
  end

The book tries to explain the reason for the error in an earlier section, but again I don’t understand the explanation.

We’ve told the double in the first example to expect puts "Welcome to Codebreaker!" and we’ve satisfied that requirement, but we’ve only told it to expect "Welcome to Codebreaker!" It doesn’t know anything about "Enter guess:"

Similarly, the double in the second example expects "Enter guess:" but the first message it gets is "Welcome to Codebreaker".

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:23:41+00:00Added an answer on June 12, 2026 at 12:23 pm

    When you create a double with output = double('output') and then pass it to the new game in Game.new(output), that double will receive every message that it is passed in the codebreaker game code. You haven’t included it, but the start method has the following code:

    module Codebreaker
      class Game
        ...
          def start
            @output.puts 'Welcome to Codebreaker!'
            @output.puts 'Enter guess:'
          end
      end
    end
    

    Here, remember that the double output has been assigned to the instance variable @output in the game‘s initialize method, so in each spec it is called with two messages, first with ‘Welcome to Codebreaker!’, then with ‘Enter guess:’.

    Without as_null_object, the output double will fail when it receives anything other than what it expects, i.e. in the first spec anything other than ‘Welcome to Codebreaker!’ and in the second spec anything other than ‘Enter guess:’. By using as_null_object on the double, you tell it to sit and wait and ignore anything other than what it expects. This avoids the problem above.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working my way through the Android tutorials, I came across something I don't
I'm currently working my way through Accelerated C++ and just came across this in
I working my way through a C++ and Operating Systems book and I've come
I'm currently working my way through this book: http://www1.idc.ac.il/tecs/ I'm currently on a section
While working my way through Cay S. Horstmann's Scala for the Impatient, I noticed
Thanks for any thoughts. while I am working my way through some custom validationAttributes,
So, while working my way through Scala for the Impatient I found myself wondering:
I am working my way through a book called Head First C#. It doesnt
I'm working my way through the Node Beginner Tutorial while tailoring things to work
Is there a way to alter the cache write policy while working with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.