Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#ValidationResult值相等断言因Assert.AreEqual而失败_C#_Unit Testing - Fatal编程技术网

C#ValidationResult值相等断言因Assert.AreEqual而失败

C#ValidationResult值相等断言因Assert.AreEqual而失败,c#,unit-testing,C#,Unit Testing,我有一个简单的ValidationRule实现: public class IntegerRangeRule : ValidationRule { public Int32 Max { get; set; } public Int32 Min { get; set; } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { int in

我有一个简单的ValidationRule实现:

public class IntegerRangeRule : ValidationRule
{
    public Int32 Max { get; set; }
    public Int32 Min { get; set; }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        int integer;

        if(string.IsNullOrEmpty((string)value))
        {
            return new ValidationResult(false, "Field cannot be empty");
        }

        if (Int32.TryParse((string) value, out integer))
        {
            if((integer < Min) || (integer > Max))
                return new ValidationResult(false, string.Format("Enter a value between {0} and {1}.", Min, Max));
        }
        else
        {
            return new ValidationResult(false, "Illegal characters: " + (string)value);
        }

        return ValidationResult.ValidResult;
    }
}
这是陷阱

前三个断言都通过了。但最后一个失败了

如果我更换

...
    if (Int32.TryParse((string) value, out integer))
    {
        if((integer < Min) || (integer > Max))
            return new ValidationResult(false, string.Format("Enter a value between {0} and {1}.", Min, Max));
    }
...
。。。
if(Int32.TryParse((字符串)值,out整数))
{
如果((整数<最小值)| |(整数>最大值))
返回新的ValidationResult(false,string.Format(“输入一个介于{0}和{1}.”,Min,Max之间的值”);
}
...
使用常量字符串

...
    if (Int32.TryParse((string) value, out integer))
    {
        if((integer < Min) || (integer > Max))
            return new ValidationResult(false, "Enter a value between 1 and 100.");
    }
...
。。。
if(Int32.TryParse((字符串)值,out整数))
{
如果((整数<最小值)| |(整数>最大值))
返回新的ValidationResult(false,“输入一个介于1和100之间的值”);
}
...
最后一个断言将通过


我不明白是什么导致了这里的问题。谢谢。

验证结果包含以下内容:

由于
ErrorContent
是一个对象,因此这是一个参考比较,而不是一个值比较。在本例中,您使用
string.Format
生成一个新字符串,并将该引用与文本进行比较,因此结果总是false。当您将其更改为相同的文字字符串时,它会传递,因为文字被CLR占用


同时,您的测试框架可能正在其
Assert.AreEqual
方法中调用
Object.Equals(Object,Object)
,该方法调用String的重写Equals方法。这是一个值比较。

谢谢你的回答,顺便说一句。Em。。。如果我一开始就可以访问源代码,肯定会有所帮助。因为ErrorContent是一个对象,所以检查引用的相等性。为什么第一个断言Assert.AreEqual(expected.ErrorContent,actual.ErrorContent);没有失败?@FrankLiu您可以访问referencesource.microsoft.com上的框架源代码。@FrankLiu我知道MSTest/VSTest使用static
Object.Equals
方法。NUnit可能会做一些不同的事情(比如检查字符串)。无论哪种方式,它都在进行值比较。对不起,我在执行Assert.AreEqual(expected.ErrorContent,actual.ErrorContent)时使用了MSTest/vstest;测试框架调用static,以便objA.Equals(objB)变成值相等检查?
...
    if (Int32.TryParse((string) value, out integer))
    {
        if((integer < Min) || (integer > Max))
            return new ValidationResult(false, "Enter a value between 1 and 100.");
    }
...
return (IsValid == vr.IsValid) && (ErrorContent == vr.ErrorContent);