How to properly unit test a class that takes a dependency using JustMock

95 views Asked by At

I'm new to unit testing and would like to know how to properly mock out an interface using JustMock Lite. I have class that looks something like this :

public class Person
{
  public Person(IFileReader reader)
  {
    Parse(reader);
  }

  public string Name {get; private set;}

  public uint Age  {get; private set;}

  private void Parse(IFileReader reader)
  {
    Name = reader.ReadString();
    Age = reader.ReadUInt();
  }
}

I have created a test method like so

[TestMethod]
public void GetAgeReturnsCorrectValue()
{
  // arrange
  var reader = Mock.Create<IFileReader>();
  var person= new Person(reader);

  // act
  var age = person.Age;

  // assert
  Assert.AreEqual(age, ???);
}

What is the proper way to write this unit test?

1

There are 1 answers

0
idursun On

You should write the arrange part of your test. I haven't used JustMock but from the examples I guess it should be something like this:

Mock.Arrange(() => reader.ReadUInt()).Returns(10);

and then your test becomes:

Assert.AreEqual(age, 10);

You arrange what is to return and assert it is indeed returned.