TrueWill
unread,Oct 5, 2008, 2:41:24 PM10/5/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Moq Discussions
Hi all,
I'm new to Moq, but have been using Rhino Mocks for some time. Rhino
does not handle one of my primary scenarios gracefully; although it
does support it, I find myself fighting the framework. That's why I'm
looking at Moq.
I frequently find myself needing to stub an interface hierarchy that
is an injected dependency of the class under test. These hierarchies
are usually complicated (manufacturing specifications, etc.). For
instance:
public interface ISetSpecification
{
IList<IComponentSpecification> ComponentSpecifications { get; }
IContainer Container { get; }
}
public interface IComponentSpecification
{
string Name { get; }
ISomeOtherSpecification Other { get; }
}
..and so on. You end up with a hierarchy of interfaces, often with
collection properties, with the lowest level having lots of read-only
properties (names, IDs, values, etc.).
To avoid duplication in the tests, I prefer to extract the hierarchy
setups to a setup fixture and/or a helper class. These end up looking
something like the following (for Moq):
public static Mock<ISetSpecification> GetSetSpec(..)
{
var mockSomeOtherSpec = new Mock<ISomeOtherSpecification>();
mockSomeOtherSpec.ExpectGet(x => x.Name).Returns(TestOtherName);
var mockComponentSpec = new Mock<IComponentSpecification>();
mockComponentSpec.ExpectGet(x =>
x.Other).Returns(mockSomeOtherSpec.Object);
var mockSetSpec = new Mock<ISetSpecification>();
// etc. - create List, add components, setup property to return list
}
Basically it's a whole bunch of wiring code that I don't want to write
over and over. Most of the simple properties are set to return
constants or parameters from the static call.
Here's the problem. (Thanks for reading this far!) Say a test uses 98%
of the setup code, but wants to change a value property on the Other
specification. I make my call to GetSetSpec, get back my high-level
mock, navigate down to the appropriate level in the hierarchy, and
get... an interface. With a read-only property. I can't call ExpectGet
on it, because it's an interface, not a Mock.
In short, I end up passing a lot of parameters in to GetSetSpec and/or
coming up with a bunch of overloads.
Any suggestions are welcome.
Thanks much,
Bill Sorensen