[Rspec-devel] Mock feature thoughts

David Astels dastels at daveastels.com
Mon Aug 28 16:17:33 EDT 2006

Hash: SHA1

On 28-Aug-06, at 4:44 PM, Judson Lester wrote:

> Given the discussion elsewhere on the list about Mocks and Rails, I
> thought I'd briefly raise a couple of feature thoughts that I'm not
> positive about in terms of philosophy.  Basically these all come  
> out of
> a basic frustration I've always had with every mock package.  That  
> mocks
> don't behave like the objects they're mocking.

That might be your problem.  You're not mocking objects.. or even  
classes.. you're mocking interfaces (whether literally as in Java, or  
conceptually in a real.. ahem dynamic language

> Specifically, if I write something like
> thing_mock=mock("thing")
> thing_mock.should_receive(:a_message).and_return("message received")
> I'd love to have thing_mock implicitly do something like
> thing_mock.should_receive(:respond_to?).with(:a_message).and_return 
> (true).any_number_of_times
> Unless, of course I were to explicitly specify some other behavior for
> #respond_to?
> Relatedly, I'd love to be able to say (and _please_ don't read this  
> as a
> request for auto-mocking or whatever - assume that there is no  
> "MyClass"
> defined anywhere)

That's often the truth of it

> thing_mock=mock("thing")
> thing_mock.mock_ancestors(["Object", "MyParentClass", "MyClass"])

part of the joy of dynamic languages is that that stuff doesn't  
matter 99% of the time.  You focus should be "what it does" not "what  
it is"

> and have it respond intelligently to #class, #kind_of, etc.

If you need that.. you mock it.  You shouldn't need it.

> This is especially useful when specifying a behavior that relies on
> another library.  For instance, DRb.

In cases like that you should create a thin facade layer to the  
library and mock that.  Another advantage of doing that is that the  
layer can also be an adapter... translating the library's API into  
something that fits better with the domain of the application.  It  
also insulates your app from changes in the library's API.. whether  
due to a new version.. or swapping out for a different solution  

> Does this seem useful, or does it expose an underlying misapprehension
> about BDD on my part?

A misunderstanding of Mock Objects and their purpose possibly.


Version: GnuPG v1.4.3 (Darwin)


More information about the Rspec-devel mailing list