我有一个类,它的属性是type。我如何为单元测试设置这个?
示例类:
public class School
{
public string Name { get; set; }
public Type SchoolType { get; set; }
}
我可以模拟Name,但我不确定如何模拟SchoolType属性。
示例测试:
string expectedName = "Richmond";
var expectedType = ???
var school = new School();
school.Name = expectedName;
school.SchoolType = expectedType;
Assert.AreEqual(expectedName, school.Name);
Assert.AreEqual(?, school.SchoolType);
您可以这样做,在这里HighSchool
是您想要分配给SchoolType
属性的类。
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);