Stubbing interface hierarchy

33 views
Skip to first unread message

TrueWill

unread,
Oct 5, 2008, 2:41:24 PM10/5/08
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

Daniel Cazzulino

unread,
Oct 6, 2008, 1:54:46 AM10/6/08
to moq...@googlegroups.com
>get... an interface.

from it, just do: Mock.Get(theInterface) and you'll get back something you can set further expectations on :)

TrueWill

unread,
Oct 6, 2008, 11:47:07 AM10/6/08
to Moq Discussions
That is EXACTLY what I wanted, and it performs as advertised. Thank
you very much!

Daniel Cazzulino

unread,
Oct 6, 2008, 11:52:39 AM10/6/08
to moq...@googlegroups.com
will update the quickstart

kzu

unread,
Oct 8, 2008, 12:15:12 AM10/8/08
to Moq Discussions
Also, note that the latest version supports auto-mocking of the
properties path like so:

var specMock = new Mock<ISetSpecification>();
specMock.Expect(s => s.Container.Name).Returns("cool!");

note the s.Container will be automatically mocked :)

On Oct 6, 12:52 pm, "Daniel Cazzulino" <dan...@cazzulino.com> wrote:
> will update the quickstart
>
Reply all
Reply to author
Forward
0 new messages