Overview:
As the title says, the PropertyConstraint can cause unexpected behavior in some cases because Actual<T>(T) is called with an object type argument instead of the property’s actual type:
|
_propValue = property.GetValue(actual, null); |
|
var baseResult = BaseConstraint.ApplyTo(_propValue); |
|
return new PropertyConstraintResult(this, baseResult); |
Example:
One example of unexpected behavior is an interaction with DefaultConstraint, in which the assert fails even though the property is default:
[Test]
public void Example()
{
var actual = new Foo
{
Bar = 0
};
Assert.Multiple(() =>
{
// passes
Assert.That(actual.Bar, Is.Default);
// fails
Assert.That(actual, Has.Property(nameof(Foo.Bar)).Default);
});
}
Assert.That(actual, Has.Property(nameof(Foo.Bar)).Default)
Expected: property Bar default
But was: 0
Proposed Solution:
Because IConstraint.Actual<T>(T) is a generic method, it's a bit awkward; we'd have to set the generic parameter and call it with reflection at runtime.
I'll link a pull request for it in a bit.
Overview:
As the title says, the
PropertyConstraintcan cause unexpected behavior in some cases becauseActual<T>(T)is called with anobjecttype argument instead of the property’s actual type:nunit/src/NUnitFramework/framework/Constraints/PropertyConstraint.cs
Lines 70 to 72 in 8a94a49
Example:
One example of unexpected behavior is an interaction with
DefaultConstraint, in which the assert fails even though the property is default:Proposed Solution:
Because
IConstraint.Actual<T>(T)is a generic method, it's a bit awkward; we'd have to set the generic parameter and call it with reflection at runtime.I'll link a pull request for it in a bit.