Because mock frameworks such as EasyMockNET can only mock interfaces
and classes derived from MarshalByRefObject.
This sounds like a practical constraint imposed by the current
implementation, not a theoretical one. Still,
it is a problem. Was this done because they needed to communicate with the
object across an appdomain boundary? If so, this probably wont be changed.
If you're building your own mock objects you can derive from an
abstract base class, but I prefer to get frameworks to do all that at
runtime
I agree about getting the framework to do the work. However, they could have
defined an abstract base class that itself derived from MBRO.
(Mocking is great, but seems to be less well known than it should be.
The current .NET implementations (EasyMockNET and NMock) are clunky in
a few ways - I intend to fix that when I get some time. Possibly in
December I may start the new project...)
I've used mocking (NMock) but I have used it less-and-less lately. A tool
that autogenerated the
mock object would make it much easier to use; handcoding the mock methods is
tedious. Like all tools/techniques, this one has its pros and cons. For me
the theory of it tended to be better then actually using it.
Another problem is that my understanding of the object that needs to be
mocked is limited, and testing production code against my own misconceptions
does little to validate the correctness of the system. I have started
writing tests against the actual running system using known data files - a
hybrid unit test and integration test. The downside is that the unit test
must account for a number of wildly different environments; e.g. debug,
release, running under the GUI versus running under the console, and also
account for delay signed builds on machines with skip verification turned on
versus resigned versions with skip verification on the build machine turned
off. The upside is that I learned an awful lot about what doesn't work
Unit testing is worth the extra time it takes to write but I cannot say the
same about mock objects.
What improvements would you make to NMock?
Not sure I agree with that, given that you can implement multiple
interfaces but you can't derive from multiple base classes.
I agree - I had forgotten about that. It's one of the biggest drawbacks to
it. OTOH, there are many
designs where this does not impose a burden.
Don't forget - if you use an interface as API (i.e. things require
parameters which are interfaces, and return interfaces) you can always
use abstract classes to implement those interfaces. The reverse is not
true.
Not quite sure what you mean here.
I agree that an abstract base class can be derived from/implement
an interface and that an interface cannot be derived from an abstract base
class, but isn't that
more a limitation of an interface and not of an abstract base class?
I find that one large advantage of using base classes is the ability to
modify the base class (add methods, properties, etc.) without breaking
existing derived classes. I feel that as time passes and systems evolve the
issues of version resilience and version compatibility, both forward and
backward, become more important, and that interfaces become a burden.