as_null_object() public

No documentation

This method has no description. You can help the RSpec community by adding new notes.

Show source
Register or log in to add new notes.
June 3, 2013 - (1.3.0 - 1.3.1)
1 thank

as_null_object working

It only listen for the messages we tell it to expect and ignore any other messages.

For example:

spec/codebreaker/game_spec.rb

module Codebreaker
 describe Game do
   describe "#start" do
     before(:each) do
       @output = double('output').as_null_object
       @game = Game.new(@output)
     end

     it "sends a welcome message" do
       @output.should_receive(:puts).with('Welcome to Codebreaker!')
       @game.start
     end
     it "prompts for the first guess" do
       @output.should_receive(:puts).with('Enter Guess:')
       @game.start
     end
   end
 end
end

In first example we are expecting ‘Welcone to Codebreaker!’ while in second example we expect ‘Enter Guess:’

and in before(:each) first line we are using as_null_object which allowing us to only check if expected string exists in game.start method then it will pass.

lib/codebreaker/game.rb

module Codebreaker
 class Game
   def initialize(output)
     @output = output
   end
   def start
     @output.puts 'Welcome to Codebreaker!'
     @output.puts 'Enter Guess:'
   end
 end
end