Closed
Description
While playing around with the Constraint Assertion feature of NUnit, my team mates made the following remark.
When combining Null and GreaterThan (or similar constraint) the order of writing is critical when used with Or condition.
Take following examples:
[TestCase(10)]
[TestCase(null)]
public void ConstraintNUnitNull(int? i)
{
Assert.That(i, Is.Null.Or.GreaterThan(9));
}
[TestCase(10)]
[TestCase(null, ExpectedException = typeof(ArgumentException))]
public void ConstraintNUnitNullFail(int? i)
{
Assert.That(i, Is.GreaterThan(9).Or.Null);
}
From the perspective of fluent reading, both should work and behave the same. But in the second case, it throws an exception.
Technically I can understand why the exception is thrown, and I'm not sure this should be logged as a bug or that lazy evaluation should be documented.
I'm using NUnit Version 2.6.3.13283 and I also quickly tested the case when using Compound Constraints, it's the same...
[TestCase(10)]
[TestCase(null)]
public void ConstraintOperatorNUnitNull2(int? i)
{
Assert.That(i, Is.Null | Is.GreaterThan(9));
}
[TestCase(10)]
[TestCase(null, ExpectedException = typeof(ArgumentException))]
public void ConstraintOperatorNUnitNull1(int? i)
{
Assert.That(i, Is.GreaterThan(9) | Is.Null);
}