Stubbing with Rhino Mocks
Posted by Davy Brion on April 7th, 2008
Rhino Mocks is an excellent mocking framework, but it makes simple stubbing very easy as well… I’m working on some code that has to retrieve data from AzMan services. These classes are basically just horrible wrappers around legacy COM stuff so they’re not really easy to work with. Luckily, these classes all return interface types so it is easy to mock/stub.
So for one of my tests, i wanted to create an IAzApplication instance which had an IAzScopes collection with 2 IAzScope instances. The IAzScopes interface pretty much only offers an indexer property, a Count property, and the GetEnumerator method. There’s also no concrete AzScopes type or something that you can create yourself for easy testing. Oh, and the IAzApplication interface defines no way of setting the IAzScopes instance to use… Joy!
Enter Rhino Mocks:
public static IAzApplication CreateAzApplicationWithTwoAzScopes(string name, string scope1, string scope2)
{
MockRepository mockRepo = new MockRepository();
IAzApplication app = mockRepo.Stub<IAzApplication>();
IAzScopes scopes = mockRepo.Stub<IAzScopes>();
var fakeScopes = new []
{
CreateAzScope(scope1, null, mockRepo),
CreateAzScope(scope2, null, mockRepo)
};
SetupResult.For(scopes.GetEnumerator()).Return(fakeScopes.GetEnumerator());
SetupResult.For(scopes.Count).Return(fakeScopes.Count());
SetupResult.For(app.Scopes).Return(scopes);
mockRepo.ReplayAll();
return app;
}
public static IAzScope CreateAzScope(string name, string description, MockRepository repo)
{
IAzScope scope = repo.Stub<IAzScope>();
scope.Description = description;
scope.Name = name;
return scope;
}
public static IAzScope CreateAzScope(string name, string description)
{
return CreateAzScope(name, description, new MockRepository());
}
This allows me to (pretty) easily create the instances i need to perform my test:
[TestMethod]
public void Map_IAzApplicationWithScopes_ReturnsApplicationWithScopes()
{
IAzApplication azApplication =
AzManEntityHelper.CreateAzApplicationWithTwoAzScopes(“myApp”, “scope1″, “scope2″);
Application application =
IAzApplicationToApplicationMapper.Map(azApplication, new AzManSidHelper(),
“domain”, new MemberRepository());
Assert.IsNotNull(application.GetChildren<Scope>().FirstOrDefault(s => s.Name == “scope1″));
Assert.IsNotNull(application.GetChildren<Scope>().FirstOrDefault(s => s.Name == “scope2″));
}
Works like a charm
July 9th, 2008 at 9:18 am
One of the problems with Rhino mocks is that they distinguish between stubs and mocks and that people who don’t have the theoretical background may use them completely the wrong way: http://www.clariusconsulting.net/blogs/
Personally I prefer Moq.
July 9th, 2008 at 9:21 am
That’s a valid point… haven’t looked at Moq yet but heard a lot of great things so far.
July 9th, 2008 at 9:32 am
You should. I think you’ll love it.
July 9th, 2008 at 9:32 am
Even though it’s not by Ayende
July 9th, 2008 at 9:44 am
hah, that’s never been a requirement