c# - 具有 ExpectedException 的 XUnit 和 MSTest 返回不同的结果

标签 c# mstest xunit yield-return expected-exception

我有一个项目正在使用 yield return,但我不明白为什么 XUnit 在我的单元测试中未能在 MSTest 通过时捕捉到异常。

这是我的伪代码。

奇怪的是,如果我采用我的私有(private)方法 EnumerableYieldReturn,并将该逻辑直接放在我的公共(public)方法 YieldReturnList 中,结果会随着 XUnit 测试通过和 MSTest 失败而翻转。

[TestClass]
public class MSTestRunner
{
    [TestMethod]
    [ExpectedException(typeof(ArgumentException))]
    public void MSTestUsingExpectedException()
    {
        var sut = new YieldReturn();

        sut.YieldReturnList(null);
    }
}

public class XUnitRunner
{
    [Fact]
    [ExpectedException(typeof(ArgumentException))]
    public void XUnitUsingExpectedException()
    {
        var sut = new YieldReturn();

        sut.YieldReturnList(null);
    }
}

public class YieldReturn
{
    public IEnumerable<string> YieldReturnList(int? value)
    {
        if (value == null)
            throw new ArgumentException();

        return EnumerableYieldReturn((int)value);
    }

    private IEnumerable<string> EnumerableYieldReturn(int value)
    {
        var returnList = new List<string>() { "1", "2", "3" };

        for (int i = 0; i < value; i++)
        {
            yield return returnList[i];
        }
    }
}

我可以通过从 sut.YieldReturnList 分配返回对象并尝试遍历它来让它们都通过,但这并不能解释为什么一个框架通过而另一个框架失败...

最佳答案

“xUnit.net 已经废除了 ExpectedException 属性以支持 Assert.Throws。”来自 https://xunit.github.io/docs/comparisons.html .

结果翻转的原因是不再抛异常所以:

MSTest:因为它使用了属性而期望异常,因此失败,因为它没有得到异常

XUnit:忽略 expect 异常属性,因为框架不使用它,因此通过,因为异常没有导致测试失败。

更改方法后不再抛出异常的原因更为复杂,但它基本上与为使用 yield 关键字的方法创建状态机有关。目前,您的公共(public)方法不直接使用 yield 关键字,因此它被视为普通函数,因此会在调用该方法后立即执行 null 检查并抛出异常。将 yield 关键字移动到公共(public)方法使其成为惰性状态机,因此它不会执行 null 检查以抛出异常,直到您开始迭代 IEnumerable。

关于c# - 具有 ExpectedException 的 XUnit 和 MSTest 返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41729938/

相关文章:

c# - Winform 数据绑定(bind)

c# - 无法将类型为 'System.__ComObject' 的 Com 对象转换为接口(interface)类型 VsRunningDocumentTable4

c# - 如何使用 linq 加入多对多关系

c# - 直接导入到 CSS for asp.net 元素的颜色选择实用程序。

c# - MSTest 未在汇编中发现单元测试

.net - 如何在 MSTest 命令行中指定 .runsettings 文件?

dll - 为什么 MSTest 不复制引用的项目库?

ruby - 向 Ruby 的内核类添加 assert() 方法是 Ruby 惯用的做法吗?

c# - JetBrains.ReSharper.TaskRunnerFramework.dll 中的“System.IO.EndOfStreamException”

design-patterns - 什么是 'Churchillian knock-off' ?