nunit icon indicating copy to clipboard operation
nunit copied to clipboard

Question: How to show Description for ConstraintBuilder

Open EvgenyMarchuk opened this issue 8 months ago • 0 comments

Hello! I have a question about ConstraintBuilder. I get an error: InvalidOperationException("A partial expression may not be resolved") when I try to resolve IResolveConstraint.

How to show Description Is.Not.Null.And.Not.Emty or Is.Not.Empty? After Resolve I get null or empty Assertion for not null/emapty value Thanks!

UPD: You can try next:

public class NunitConstraintTests
{
    [Test]
    public void ResoleConstraintTest()
    {
        new Validator()
            .Validate("not null str", Is.Not.Null)
            .Validate("not empty str", Is.Not.Empty)
            .Validate(string.Empty, Is.Not.Empty)
            .Validate(string.Empty, Is.Not.Null);
    }
}

public class Validator
{
    public Validator Validate(string str, IResolveConstraint expression)
    {
        var resolve = expression;
        var stepName = $"Check String: Actual {str}, Expected {resolve}";
        TestContext.WriteLine($"STEP: {stepName}");
        Assert.That(str, expression, stepName);
        return this;
    }
}

In the console you get this message:

STEP: Check String: Actual not null str, Expected <unresolved <null>>
STEP: Check String: Actual not empty str, Expected <unresolved <empty>>
STEP: Check String: Actual , Expected <unresolved <empty>>

<unresolved <null> or <unresolved <empty> but I'm using Not.Null/Empty So, If you add to the method the folowing part, the Null Assert will be for the first string "not null str".

var resolve = expression.Resolve().Description;

EvgenyMarchuk avatar Jun 21 '24 12:06 EvgenyMarchuk