小编Kyl*_*yle的帖子

为什么这个断言在比较结构时抛出格式异常?

我试图断言两个System.Drawing.Size结构的相等性,我得到一个格式异常而不是预期的断言失败.

[TestMethod]
public void AssertStructs()
{
    var struct1 = new Size(0, 0);
    var struct2 = new Size(1, 1);

    //This throws a format exception, "System.FormatException: Input string was not in a correct format."
    Assert.AreEqual(struct1, struct2, "Failed. Expected {0}, actually it is {1}", struct1, struct2); 

    //This assert fails properly, "Failed. Expected {Width=0, Height=0}, actually it is {Width=1, Height=1}".
    Assert.AreEqual(struct1, struct2, "Failed. Expected " + struct1 + ", actually it is " + struct2); 
}
Run Code Online (Sandbox Code Playgroud)

这是预期的行为吗?我在这里做错了吗?

c# unit-testing mstest exception-handling string-formatting

94
推荐指数
3
解决办法
4115
查看次数