1

I am learning Minimal APIs (and also still learning C#), but just got stuck when creating Unit Tests. Given this function:

public IResult GetGenderById(Guid id)
    {
        var gender = this.repository.Get(id);
        if (gender != null)
        {
            return Results.Ok(gender);
        }
        return Results.NotFound();
    }

I can easily assert the IResult, for example: Assert.That(result, Is.InstanceOf<Ok<GenderData>>()); or Assert.That(result, Is.InstanceOf<NotFound>());.

However, if I was to return a message with the NotFound, I suddenly cannot assert for this IResult, thanks to the Anonymous Type:

public IResult GetGenderById(Guid id)
    {
        var gender = this.repository.Get(id);
        if (gender != null)
        {
            return Results.Ok(gender);
        }
        return Results.NotFound(new { Message = "No 'Gender' found with the given ID." });
    }

One of the solutions I have found is to run Integrations Tests instead, where I can directly check the returned JSON, but I am hoping to Unit Test this method, and later on to run Integration Tests. I cannot find many resources online regarding Minimal APIs (compared to MVC and Controllers).

Thank you!

1 Answer 1

2

If you don't want to introduce a type for the result (though with record's it is very easy and I would go this way) you have several workarounds.

You can check for the status code:

var sutResult = Results.NotFound(new { Message = "No 'Gender' found with the given ID." });

Assert.That(sutResult, Is.AssignableTo<IStatusCodeHttpResult>());
Assert.That((sutResult as IStatusCodeHttpResult).StatusCode,Is.EqualTo((int)HttpStatusCode.NotFound));

Or use some reflection:

Assert.That(sutResult.GetType().GetGenericTypeDefinition(), Is.EqualTo(typeof(NotFound<>)));

Probably combined with reflection based asserts (might require to remove warnings):

Assert.That(sutResult, Has.Property("Value").Property("Message").Contains("Gender"));
Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping to not introduce more records (and keep the API as minimal and simple as possible), but it seems to be the most straightforward answer. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.