无法找到“断言”。“失败”和“断言”。通过或等效

我曾经在 NUnit 中使用过这些,它们非常有用?

编辑,代码示例:

bool condition = false;//would be nice not to have this
observable.Subscribe(_ =>
{
if (real test)
condition= true;//Assert.Pass()
});
StartObservable();
Assert.True(condition);//Assert.Fail()
32621 次浏览

文件包括 比较图,其中包括:

Net 替代方案: Assert.True(false, "message")

(它没有显示 Assert.Pass,我自己也从来没有使用过,但我怀疑另一种选择只是从测试中返回。当然,如果您想在嵌套方法调用中抛出它,那么这样做没有任何帮助。我的 怀疑是因为它在 NUnit 中不常用,所以在比较图中没有它。)

只需抛出一个异常来满足两个需求(退出嵌套循环和缺少的断言的替代方案)。失败方法)。唯一的问题是没有合适的基本异常(例如 TestException)可以用来避免获得关于使用基本 Exception 类的警告,所以更直接的例如 InvalidOperationException 可能是一个不错的选择。

XUnit 文档提出的 Assert.Fail("messsage")的替代方案

Net 替代方案: Assert.True (false,“ message”)

它的输出是

message


Expected: True
Actual:   False

摆脱

Expected: True
Actual:   False

不要调用 Assert.True(false, "message"),而是抛出 Xunit.Sdk.XunitException
例如,创建一个与下面类似的 helper 方法:

public static class MyAssert
{
public static void Fail(string message)
=> throw new Xunit.Sdk.XunitException(message);
}

获得 Pass 断言的另一种方法是创建新的断言。

public static class ExtraAssert
{
public static void Pass(this Microsoft.VisualStudio.TestTools.UnitTesting.Assert assert)
{
return;
}
}

但是有一个问题,因为您只能使用“ That”关键字访问该方法。

Assert.That.Pass();

档号: https://www.meziantou.net/mstest-v2-create-new-asserts.htm