Question

I have a test double that I'd like to be able to receive any message.

I know I can expect the double to receive a certain message and return a value like so:

foo = double()
allow(foo).to receive(:bar) { "Foobar" }

I can also allow foo to receive any message using #as_null_object like:

foo = double()
foo.as_null_object

Is there any other syntax for this? Seems I should be able to do something like:

allow(foo).to receive(:anything)
Was it helpful?

Solution

allow and expect methods can be used to stub methods/set expectations on particular method. Augmenting object with null object pattern is quite different, and thus uses different method call.

Please note that you should usually not use null object in area that is tested by particular test -- it is meant to imitate some part of the system that is side effect of tested code, which cannot be stubbed easily.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top