[rspec-devel] [ rspec-Patches-8303 ] :nil_object option for mocks

noreply at rubyforge.org noreply at rubyforge.org
Tue Jan 30 12:09:38 EST 2007

Patches item #8303, was opened at 2007-01-30 07:04
You can respond by visiting: 

Category: mock module
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Chris Anderson (jchris)
Assigned to: Nobody (None)
Summary: :nil_object option for mocks

Initial Comment:
A nil object returns nil (rather than itself) for any methods which aren't stubbed.

This is useful if you are going to send something into views, and don't care how it renders, but don't want errors.


>Comment By: Chris Anderson (jchris)
Date: 2007-01-30 17:09

I've been using nil objects as an implementation in Ruby of the null object 
pattern for a while now. Both approaches have their uses, depending on the 
client code.

You're right, the RSpec API for the nil object could have a more transparent 
usage that reflects its status as a variant on the null object pattern. Maybe as 
an alternative value for :null_object. My first thought would be 

mock('returns nil for all unstubbed methods', :null_object => :returns_nil)

This keeps the surface area of mocks down, and presents the "nil object" as a 
special case of null object.

Thoughts? Should I bring this up on the mailing list? Once we figure out the 
API, I'll be happy to provide a patch.


Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-01-30 12:18

The term "null object" refers to a design pattern (google it).
There is no "nil object" design pattern.

I'm a little concerned that the presence of :null_object and :nil_object may be confusing for users since they sound so similar. One is the implementation of a design pattern, the other is ehm, something else.

Can we come up with a less confusing name?


You can respond by visiting: 

More information about the rspec-devel mailing list