Helper TestUserInterface

As part of a unit test of one of the InventoryCommand implementations, we do not want to test the referenced dependencies. Fortunately, because the commands adhere to the DIP, we can create a helper class to validate the implementation interactions with the dependencies. One of the dependencies is the IUserInterface, which is passed into the implementation in the constructor. The following is a reminder of the methods of the interface:

public interface IUserInterface : IReadUserInterface, IWriteUserInterface { }

public interface IReadUserInterface
{
string ReadValue(string message);
}

public interface IWriteUserInterface
{
void WriteMessage(string message);
void WriteWarning(string message);
}

By implementing a helper class, we can supply the information required by the ReadValue method as well as verify that the appropriate messages are received in the WriteMessage and WriteWarning methods. In the test project, a new class called TestUserInterface was created that implements the IUserInterface interface. The class contains three lists containing the expected WriteMessage, WriteWarning, and ReadValue calls, and keeps track of the number of times it has been called.

For example, the WriteWarning method is shown as follows:

public void WriteWarning(string message)
{
Assert.IsTrue(_expectedWriteWarningRequestsIndex < _expectedWriteWarningRequests.Count,
"Received too many command write warning requests.");

Assert.AreEqual(_expectedWriteWarningRequests[_expectedWriteWarningRequestsIndex++], message, "Received unexpected command write warning message");
}

The WriteWarning method performs two asserts. The first verifies that the method is not called more times than expected and the second verifies that the message received matches the expected message.

The ReadValue method is similar but it additionally returns a value back to the calling InventoryCommand implementation. This will simulate the user entering information into the console:

public string ReadValue(string message)
{
Assert.IsTrue(_expectedReadRequestsIndex < _expectedReadRequests.Count,
"Received too many command read requests.");

Assert.AreEqual(_expectedReadRequests[_expectedReadRequestsIndex].Item1, message,
"Received unexpected command read message");

return _expectedReadRequests[_expectedReadRequestsIndex++].Item2;
}

 As an extra validation step, at the end of a test method, the TestUserInterface is called to verify that the expected number of ReadValue, WriteMessage, and WriteWarning requests were received:

public void Validate()
{
Assert.IsTrue(_expectedReadRequestsIndex == _expectedReadRequests.Count,
"Not all read requests were performed.");
Assert.IsTrue(_expectedWriteMessageRequestsIndex == _expectedWriteMessageRequests.Count,
"Not all write requests were performed.");
Assert.IsTrue(_expectedWriteWarningRequestsIndex == _expectedWriteWarningRequests.Count,
"Not all warning requests were performed.");
}

The TestUserInterface class illustrates how a dependency can be mocked to provide stubbed functionality as well as provide assertions to help verify the expected behavior. In later chapters, we will use a third-party package to provide a more sophisticated framework for mocking the dependencies.