Question

Should I be unit testing an app service as simple as the one shown below?

public void Update(UserAccountViewModel viewModel)
    {
        var instance = Mapper.Map<UserAccountViewModel, UserAccount>(viewModel);
        _userAccountService.UpdateInstance(instance);
    }

As you can see it simply takes in a view model, uses AutoMapper to map it to a domain model and calls a domain service method to update the object. If I should be unit testing it I'm struggling to see what to unit test! That perhaps answers my own question as you could argue that there's nothing really to test other than AutoMapper's work which I believe I should not be unit testing anyway as its someone else's work right?

Edit: Thanks for the replies. I've gone with something like this which simply tests that the UpdateInstance method is called once:

[TestMethod]
public void GivenViewModelWhenEditingAUserThenTheUpdateInstanceDomainServiceMethodIsCalledOnce()
{
    // Arrange
    _userAccounts.Clear();
    var viewModel = new Mock<UserAccountViewModel>();

    // Act
    _userAccountAppService.Update(viewModel.Object);

    // Assert
    _userAccountService.Verify(e => e.UpdateInstance(It.IsAny<UserAccount>()), Times.Once());
}
Was it helpful?

Solution

"What to unit test" is a really subjective question. Answers will range from "nothing; it's a waste of time" to "always 100% code coverage." I favor pragmatic unit testing. In this case, the Update method does so little by itself that, IMO, it's not worth unit testing. You'd just be testing AutoMapper, which should already be tested by its creators.

Unit testing the real user account service's UpdateInstance method is most likely valuable, though.

OTHER TIPS

It's not about making sure that every public method in every class has a test wrapped around it. It's more important to make sure that all behaviors are covered.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top