简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM