我正在编写用于测试单元测试值的扩展方法。一个天真的例子是:
public static void ShouldBeTrue(this bool value)
{
if(!value)
{
throw new AssertFailedException("Expected true");
}
}
并在测试中使用它:
someBool.ShouldBeTrue();
一切正常,除了抛出异常的行将是我在测试结果窗口中双击失败的测试时最终出现的行,并且在测试结果详细信息中,抛出行显示在错误堆栈跟踪中。
有没有办法解决这个问题,以便“someBool.ShouldBeTrue();”:
最佳答案
已经为此编写了一个类库:http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx
上面的链接引用了一段话:
... For reference, those unavailable Asserts are:
- Assert.IsNaN
- Assert.IsEmpty
- Assert.IsNotEmpty
- Assert.Greater
- Assert.GreaterOrEqual
- Assert.Less
- Assert.LessOrEqual
- Assert.IsAssignableFrom
- Assert.IsNotAssignableFrom
- CollectionAssert.IsEmpty
- CollectionAssert.IsNotEmpty
- StringAssert.AreEqualIgnoringCase
- StringAssert.IsMatch
- FileAssert.AreEqual
- FileAssert.AreNotEqual
...I have created a class library that includes all of them except the FileAssert methods and StringAssert.IsMatch. ... You can download the class from my SkyDrive public folder: https://skydrive.live.com/?cid=93d618d639ec9651&id=93D618D639EC9651%211283
关于.net - 如何使用 "proper"调用堆栈创建自定义 MSTest 断言方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4215513/