Home > database >  How to set a System.Type property for unit testing?
How to set a System.Type property for unit testing?

Time:01-17

I've got a class that has a property of type Type. How can I set this for unit testing?

Example class:

public class School
{
    public string Name { get; set; }
    public Type SchoolType { get; set; }
}

I can mock the Name but I'm not sure how to mock the SchoolType property.

Example test:

string expectedName = "Richmond";
var expectedType = ???

var school = new School();
school.Name = expectedName;
school.SchoolType = expectedType;

Assert.AreEqual(expectedName, school.Name);
Assert.AreEqual(?, school.SchoolType);

CodePudding user response:

You can do this, In here HighSchool is a class that you want to assign to SchoolType property.

string expectedName = "Richmond";
var expectedType = typeof(HighSchool);

var school = new School();
school.Name = expectedName;
school.SchoolType = expectedType;

Assert.AreEqual(expectedName, school.Name);
Assert.AreEqual(expectedType, school.SchoolType);
  • Related