mercredi 22 avril 2015

Unit testing a nested method

Say I have a service like so:

public class MyService : IMyService
{
    public void DoStuff(IDependency dependency, string value)
    {
        dependency.SomeMethod(value, true);
        DoOtherStuff(dependency);
    }

    public void DoOtherStuff(IDependency dependency)
    {
        // do some stuff
    }
}

Now, when unit testing MyService, I can mock the dependency easily enough and test the dependency is properly used:

public void MyServiceTest()
{
    // Arrange 
    var mockDependency = new Mock<IDependency>();
    mockDependency.Setup(m => m.SomeMethod());
    var service = new MyService();

    // Act
    service.DoStuff(mockDependency.Object, "value");

    // Assert
    mockDependency.Verify(v => v.SomeMethod(), Times.Once);
}

How do I test that the service calls DoOtherStuff? Or is this a bad pattern? What would be the correct way to do this?

Aucun commentaire:

Enregistrer un commentaire