在单元测试方面,我是一个全新的,所以请原谅我,如果这完全是无知的话.但即使我为预期和实际提供完全相同的值,我也无法通过单元测试.当我设置断点并逐步执行时,我已经确认预期变量和实际变量都是包含两个项目的字符串数组,blah和blah.但每次说“Assert.AreEqual失败时,测试仍然失败.预期:System.String []实际: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,它不会被数组覆盖.换句话说,这将打印错误:
var x = new[] { 0 };
var y = new[] { 0 };
Console.WriteLine(x.Equals(y));
您应该使用CollectionAssert代替集合:
CollectionAssert.AreEqual(expected,actual);