4

I've been working on a large project where much of the code is not under test. I have been able to add some unit tests to functional areas of the code, and also to start mocking services but I have a question about how much mocking to do on other areas.

This application and endpoints are primary based on .NET remoting so there are a lot of "data objects" that are being manipulated over the channel.

In testing some functions that operate on the object I'm unsure if I should be mocking the data object or instantiating one.

Here is an example:

[TestMethod()]
public void Test_GetAgeTest()
{
    //Arrange
    var familyMemberData = new FamilyMemberData
    {
        Demographics = { DoB = new DVDateTime(2000, 01, 01, 0, 0, 0) }
    };
    var year = DateTime.Today.Year;

    //Act
    var age = Translators.Test_GetAge(familyMemberData);

    //Assert
    Assert.AreEqual(year - 2000, age);
}

Translators is a static class, and GetAge is a function that does some calculation on the FamilyMemberData to get the age (Test_GetAge exposes it publicly).

Should FamilyMemberData be a Mock Object, or a Fake, or in cases like these just use them as is?

7

There are several reasons to use Mocks/Fakes/Stubs (putting them all together, although they really are different things):

  • You want to have more control over the responses of a dependency, so that you can more easily test situations that are hard to recreate with the real objects
  • You want to isolate your tests from slow, possibly unreliable network connections
  • You want to make it easy on yourself to make sure that changes made by a test do not persist beyond the test
  • You want to isolate the code-under-test from dependencies that could have errors in them themselves.

If none of these reasons exist for the code you want to test, then you should just use the real objects, as that is typically much simpler than arranging for a mock, fake or stub.

In your case FamilyMemberData appears to be just a simple data container without complex logic or dependencies that might trigger any of the reasons to create a mock, so there is no value in mocking FamilyMemberData.

2

The general purpose to mocks/fakes etc is to bypass functionality in a class to simplify testing. If eg FamilyMemberData only contains data, then there is nothing to be gained from mocking it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.