[rspec-users] as_null_object

David Chelimsky dchelimsky at gmail.com
Wed May 26 23:22:41 EDT 2010


On May 26, 2010, at 10:01 PM, rhydiant wrote:

> Given that RSpec has the following methods to create test doubles ...
> 
>  double(:my_test_double)
>  mock(:my_mock_object)
>  stub(:my_stub)

double(), mock(), and stub() all return the same type of object: a test double (actually, it's a Mock, but that's for legacy reasons - the class name might change to Double in the future). The difference between "mocking" and "stubbing" is at the method level. I'd actually like to deprecate mock() and stub() in the long run to reduce the noise.

> Is there a way to/ plans to introduce a similar syntax for null
> objects?
> 
>  null_object(:my_null_object)

You could easily do this yourself, like this:

def null_object(*args)
  double(*args).as_null_object
end

I don't think I'd want to add this to rspec directly, for the noise reduction reasons I wrote above.

> Instead of mock(:bar, :is_null_object => true) or
> mock(:foo).as_null_object

FYI - :null_object => true is deprecated (you'll start seeing deprecation notices in the next beta if you're using it).

> I think this would be cleaner, what do you think?

Clean is relative. It might be slightly less typing, but I don't know that it's any more expressive, and IMO it increases the noise level. Another thing to consider is that there are other "decorators" that use this same pattern: double(name).as_something.

That all make sense?

Cheers,
David


More information about the rspec-users mailing list