I've got a simple NUnit test:
[Test]
public void Invalid_ID_throws_an_exception()
{
var zero = 0;
var negativeNumber = -9;
Assert.Throws<ArgumentOutOfRangeException>(() => edsp.PersonInfoById(zero));
Assert.Throws<ArgumentOutOfRangeException>(() => edsp.PersonInfoById(negativeNumber));
}
and the tested method:
public IEnumerable<PersonInfo> PersonInfoById(int id)
{
if (id <= 0)
throw new ArgumentOutOfRangeException(nameof(id), "ID must be greater than zero");
yield return new PersonInfo();
}
... but the test fails on the first assertion because the result is null
, rather than the expected ArgumentOutOfRangeException
:
Message:
Expected: <System.ArgumentOutOfRangeException>
But was: null
What am I doing wrong here?
Edit: also, my debugger isn't stepping into my tested method edsp.PersonInfoById
for some reason - steps right over it despite clicking "step into" when debugging.
The test passes if you force the enumeration by calling .ToList()
on the result.
For example:
[Test]
public void Invalid_ID_throws_an_exception()
{
var zero = 0;
var negativeNumber = -9;
Assert.Throws<ArgumentOutOfRangeException>(() => edsp.PersonInfoById(zero).ToList());
Assert.Throws<ArgumentOutOfRangeException>(() => edsp.PersonInfoById(negativeNumber).ToList());
}