这是你能做的吗?我收到一条错误消息,上面写着“属性参数必须是常量表达式......”我想我在某处看到属性只能将原始类型作为参数?
该消息是正确的。无需进一步解释。
这是错误的方法吗?我应该只传递属性并在测试中创建 dto 吗?
您可以使用原始类型并在测试中创建模型。
[TestMethod]
[DataRow("Leo", 22)]
[DataRow("John", null)]
public void AddStudent_WithMissingFields_ShouldThrowException(string firstName, int? age,) {
StudentDto studentDto = new StudentDto {
FirstName = firstName,
Age = age.GetValueOrDefault(0)
};
// logic to call service and do an assertion
}
或者按照评论中的建议,使用[DynamicData]属性
static IEnumerable<object[]> StudentsData {
get {
return [] {
new object[] {
new StudentDto { FirstName = "Leo", Age = 22 },
new StudentDto { FirstName = "John" }
}
}
}
}
[TestMethod]
[DynamicData(nameof(StudentsData))]
public void AddStudent_WithMissingFields_ShouldThrowException(StudentDto studentDto) {
// logic to call service and do an assertion
}