小编Mig*_*tom的帖子

如何对自定义验证属性进行单元测试

我有一个自定义的asp.net mvc类验证属性.我的问题是如何对其进行单元测试?测试类具有属性是一回事,但这实际上不会测试其中的逻辑.这是我想测试的.

[Serializable]
[EligabilityStudentDebtsAttribute(ErrorMessage = "You must answer yes or no to all questions")]
public class Eligability
{
    [BooleanRequiredToBeTrue(ErrorMessage = "You must agree to the statements listed")]
    public bool StatementAgree { get; set; }

    [Required(ErrorMessage = "Please choose an option")]
    public bool? Income { get; set; }
Run Code Online (Sandbox Code Playgroud)

.....为简洁而删除}

[AttributeUsage(AttributeTargets.Class)]
public class EligabilityStudentDebtsAttribute : ValidationAttribute
{
    // If AnyDebts is true then 
    // StudentDebts must be true or false

    public override bool IsValid(object value)
    {
        Eligability elig = (Eligability)value;
        bool ok …
Run Code Online (Sandbox Code Playgroud)

c# validation unit-testing asp.net-mvc-2

41
推荐指数
3
解决办法
2万
查看次数

标签 统计

asp.net-mvc-2 ×1

c# ×1

unit-testing ×1

validation ×1