我有一个使用收益返回的项目,但不明白为什么在 MSTest 通过时 XUnit 未能在我的单元测试中捕获异常。
这是我的虚拟代码。
奇怪的是,如果我采用我的私有方法 EnumerableYieldReturn 并将该逻辑直接放在我的公共方法 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 分配返回对象并尝试迭代它来让它们都通过,但这并不能解释为什么一个框架通过而另一个框架失败......