I have a project that is using a yield return and do not understand why XUnit is failing to catch an exception in my unit test while MSTest is passing.
Here is my dummy code.
The bizarre thing is that if I take my private method, EnumerableYieldReturn, and put that logic directly in my public method, YieldReturnList, the outcomes flip with the XUnit test passing and the MSTest failing.
[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];
}
}
}
I can get them both to pass by assigning the return object from sut.YieldReturnList and attempting to iterate through it but that doesn't explain why one framework is passing and the other is failing...