S.
Steve Freeman
http://www.mockobjects.com
Winner of the Agile Alliance Gordon Pask award 2006
I would echo what Isaiah said... in your specific example, because of
the tight coupling between the two it is probably not appropriate to
even attempt to use mock objects.
However... one of the things I've found helpful over the years is the
fact that it is often appropriate to "favor encapsulation over
inheritance". Clearly your example isn't telling us everything (and
there are probably excellent reasons for using inheritance), so what I'm
about to suggest may not be appropriate. Use at your own risk!
What you appear to be doing here is requiring that a certain method
(Bar) is provided, while also providing a default implementation. What
you could do it create an interface (IFoo) which both Foo and
ConcreteFoo implement.
In ConcreteFoo's default constructor, instantiate a (now non abstract,
but probably not available for public creation) Foo object. In your
implementation of Bar, call that object's Bar method before performing
your own operation.
Create another mechanism (typically it's an alternate constructor, often
with internal-only visibility) which allows you to specify the
underlying IFoo object.
Now you have done so, you can use RhinoMocks to create a mock "Foo"
(actually an IFoo) which returns the specific values you need. That way
you can verify the operation of ConcreteFoo's Bar operation, regardless
of what Foo itself will return.
Does this make sense, or am I a babbling idiot?
Regards,
Richard
Thanks for your help!
[Older message history trimmed]
* C O N F I D E N T I A L I T Y N O T I C E *
-----------------------------------------------------------
The content of this e-mail is intended solely for the use of the individual or entity to whom it is addressed. If you have received this communication in error, be aware that forwarding it, copying it, or in any way disclosing its content to any other person, is strictly prohibited. Quixote Traffic Corporation is neither liable for the contents, nor for the proper, complete and timely transmission of (the information contained in) this communication. If you have received this communication in error, please notify the author by replying to this e-mail immediately and delete the material from any computer.
The common question I ask myself is, "If this thing worked, how would
I know"? What are the implications of the addition by 5 that are
visible outside the object? It might be the contents of a call to a
neighbouring object, or the value of a property. That's what you
should be testing.
As the others said, mocking within an object ties the test to the
object's implementation not to its behaviour. Use sparingly.
S.
Steve Freeman