Visual Studio的单元测试Assert.AreEqual失败,相同的期望值和实际值 [英] Visual Studio Unit Testing Assert.AreEqual Fails with Identical Expected and Actual Values

查看:278
本文介绍了Visual Studio的单元测试Assert.AreEqual失败,相同的期望值和实际值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个总福利局当涉及到单元测试,所以请原谅我,如果这是完全不了解。但我不能得到这个方法传递一个单元测试,即使我提供完全相同的值,预期和实际。当我设置断点和单步,我已经证实,预期和实际变量是一个字符串数组,包含两个项目,胡说和胡说。但测试每次都声称仍未Assert.AreEqual失败的预期:System.String []实际:System.String []

I am a total newb when it comes to Unit Testing, so please pardon me if this is total ignorance. But I can't get this method to pass a unit test even if I provide the exact same values for the expected and actual. When I set breakpoints and step through, I have confirmed that both expected and actual variables are a string array containing two items, blah and blah. But the test still fails every time stating that "Assert.AreEqual failed. Expected: System.String[] Actual:System.String[]"

namespace TestProject1

{
 public class Testing
 {
    public string[] PMDate(string lastPm)
    {
        if (string.IsNullOrEmpty(lastPm))
            return new []{"blah", "blah"};
        string[] results = lastPm.Split(new[] {" "}, StringSplitOptions.None);

        if (results.Length < 2)
            return new[] {"blah", "blah"};

        return results;
    }
 }
[TestClass()]
public class UnitTest1
{
    [TestMethod()]
    public void PmDateTest()
    {
        Testing test = new Testing();
        string[] expected = test.PMDate("01/01/1900");
        string[] actual = test.PMDate("01/01/1900");
        Assert.AreEqual(expected, actual);
    }
}

}

推荐答案

您的测试将使用的Object.Equals ,这是不是对数组覆盖。换句话说,这将打印错误的:

Your test will be using object.Equals, which isn't overridden for arrays. In other words, this will print false:

var x = new[] { 0 };
var y = new[] { 0 };
Console.WriteLine(x.Equals(y));

您应该使用<一个href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.collectionassert.aspx"><$c$c>CollectionAssert而不是对集合:

You should use CollectionAssert instead for collections:

CollectionAssert.AreEqual(expected, actual);

这篇关于Visual Studio的单元测试Assert.AreEqual失败,相同的期望值和实际值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆