Skip to content

Writing Unit Tests

Emma Corbett edited this page Jun 26, 2020 · 13 revisions

Unit tests are used to test the functionality of a class in isolation. So if a class depends on another we will need to mock any interactions with that class. We are using dependency injection so any class which needs to interact with another will be passed an interface, which the depended on class subscribes to, in the constructor. In the tests, we can then just pass in a mock implementation of this interface into the constructor instead.

There is some good advice for mocking and writing unit tests here.

Setting up and using mocks

You can use mocks to return a specific value for a given input. For example a test for the following code..

public class GetSomeData
{
    public IAccessDataStore _datastore;
    public GetSomeData(IAccessDataStore dataStore)
    {
         _datastore = dataStore;
    }

    public SomeData Execute(int id)
    {
         return _dataStore.Get(id);
    }
}

might look like..

public class GetSomeDataTests
{
    [Test]
    public void ExecuteReturnsTheDataFromTheDataStore()
    {
         //Arrange
         var mock = new Mock<IAccessDataStore>();
         var stubData = new SomeData{ fieldOne: "data", fieldTwo: 7 };
         var id = 89;
         mock.SetUp(x => x.Get(89)).Returns(stubData);

         //Act
         var classUnderTest = new GetSomeData(mock.Object);
         var returnedData = classUnderTest.Execute(89);

         //Assert
         returnedData.Should().BeEquivalentTo(stubData);
    }
}

Or you can assert that they were called with the correct inputs. For example a test for the following code..

public class SaveSomeData
{
    public IAccessDataStore _datastore;
    public SaveSomeData(IAccessDataStore dataStore)
    {
         _datastore = dataStore;
    }

    public void Execute(SomeData data)
    {
         return _dataStore.Put(data);
    }
}

might look like..

public class SaveSomeDataTests
{
    [Test]
    public void ExecutePutsTheDataIntoTheDataStore()
    {
         //Arrange
         var mock = new Mock<IAccessDataStore>();
         var dataToSave = new SomeData{ fieldOne: "data", fieldTwo: 7 };

         //Act
         var classUnderTest = new SaveSomeData(mock.Object);
         classUnderTest.Execute(89);

         //Assert
         mock.Verify(x => x.Put(dataToSave), Times.Once());
    }
}

These examples are just a couple of common use cases for using Moq, check out the documentation for more examples and use cases.

Clone this wiki locally