I am getting started on the Moq framework and absolutely love it. I am writing some controller tests that have several services and interfaces to Arrange my controller for the test. I'd love to modularize it a bit more, and thought this would be a trivial task, but it turns out to be a bit trickier than I thought.
Here is one simple unit test that I have to show an example:
[Test]
public void Get_SignIn_Should_Return_View()
{
#region //TODO: figure out how to extract this out to avoid duplicate code
// Arrange
var membershipService = new Mock<IMembershipService>();
var formsService = new Mock<IFormsAuthenticationService>();
var userService = new Mock<IUserService>();
var dictService = new Mock<IDictionaryService>();
var shoppingBasketService = new Mock<IShoppingBasketService>();
//Create the service provider mock and pass in the IRepositoryFactory so that it isn't instantiating real repositories
var repoFactory = new Mock<IRepositoryFactory>();
var serviceProvider = new Mock<ServiceProvider>( (IRepositoryFactory)repoFactory.Object );
var context = new Mock<HttpContextBase> { DefaultValue = DefaultValue.Mock };
var sessionVars = new Mock<SessionVars>();
AccountController controller = new AccountController( serviceProvider.Object, sessionVars.Object )
{
FormsService = formsService.Object,
MembershipService = membershipService.Object,
UserService = userService.Object,
DictionaryService = dictService.Object,
ShoppingService = shoppingBasketService.Object
};
controller.ControllerContext = new ControllerContext()
{
Controller = controller,
RequestContext = new RequestContext( context.Object, new RouteData() )
};
#endregion
// Act
ActionResult result = controller.SignIn();
// Assert
Assert.IsInstanceOf<ViewResult>( result );
}
What I'd like to be able to do is take everything in the #region
and extract that out into a helper method or [Setup] method, but if I do that, then I don't have access to each mock service to setup expectations.
Is there something I'm missing here, or do I really have to copy-and-paste this chunk of Arrange code in each Unit test?
Try using a context to setup all your mocks, then use test fixtures that inherit your context. Put the tests inside these fixtures and violà! This code might not be exactly right for the framework you are using. If it is NUnit then it will be. But the theory is there.
public abstract class MembershipTestContext
{
var membershipService = new Mock<IMembershipService>();
var formsService = new Mock<IFormsAuthenticationService>();
var userService = new Mock<IUserService>();
var dictService = new Mock<IDictionaryService>();
var shoppingBasketService = new Mock<IShoppingBasketService>();
//Create the service provider mock and pass in the IRepositoryFactory so that it isn't instantiating real repositories
var repoFactory = new Mock<IRepositoryFactory>();
var serviceProvider = new Mock<ServiceProvider>( (IRepositoryFactory)repoFactory.Object );
var context = new Mock<HttpContextBase> { DefaultValue = DefaultValue.Mock };
var sessionVars = new Mock<SessionVars>();
[SetUp]
AccountController controller = new AccountController( serviceProvider.Object, sessionVars.Object )
{
FormsService = formsService.Object,
MembershipService = membershipService.Object,
UserService = userService.Object,
DictionaryService = dictService.Object,
ShoppingService = shoppingBasketService.Object
};
controller.ControllerContext = new ControllerContext()
{
Controller = controller,
RequestContext = new RequestContext( context.Object, new RouteData() )
};
}
[TestFixture]
public class when_getting_sign_in : MembershipContext
{
[Test]
public void Should_return_view()
{
// Act
ActionResult result = controller.SignIn();
// Assert
Assert.IsInstanceOf<ViewResult>(result);
}
[Test]
public void Should_do_another_test()
{
... another test etc
}
}
One thing you could do is use the Mock.Get
method (http://api.moq.me/html/C6B12927.htm) to retrieve the mock for a given object instance.
Another option would be to refactor your code and store references to your mock objects in instance variables of your test class (if all of the tests in the test class require them) or perhaps a simple data structure (if only some of the tests will require them).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With