Bhav
Bhav

Reputation: 2207

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

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);

Upvotes: 1

Views: 44

Answers (1)

Sachith Wickramaarachchi
Sachith Wickramaarachchi

Reputation: 5872

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);

Upvotes: 1

Related Questions