alba icon indicating copy to clipboard operation
alba copied to clipboard

Fixed the StatusCodeSuccessAssertion

Open JeffryGonzalez opened this issue 1 year ago • 1 comments

Ok - not sure this is the right approach, but I sort of messed up the StatusCodeShouldBeSuccess() Assertion last year.

I actually noticed this when recording a video with @jeremydmiller. ;(

I feel like I made too big of a change here, and don't mind rejection or feedback.

The issue is the StatusCodeShouldBeSuccess() collided with the default (StatusCodeShouldBeOk).

Since StatusCodes are pretty binary - you wouldn't have multiple checks for different status codes in the same scenario, the Scenario.cs has a IgnoreStatusCode() you can use. so in order for the StatusCodeShouldBeSuccess() to work, you'd have to do something like:

 var response = await M.Host.Scenario(api =>
        {
             api.Post.Json(request).ToUrl("/work-to-be-done");
             api.IgnoreStatusCode(); // <-- add this.
             api.StatusCodeShouldBeSuccess();    
        });

That seems weird to me.

What I propose, meekly, with this PR is (egads) a "Marker Interface" for any Assertion that deals with Status Codes. (IStatusCodeAssertion).

In the Scenario.cs's RunAssertions, you set _ignoreStatusCode to true (if it isn't already) if there are any _assertions that implement that interface.

Now, the above can be written as:

 var response = await M.Host.Scenario(api =>
        {
             api.Post.Json(request).ToUrl("/work-to-be-done");
             // api.IgnoreStatusCode(); // <-- remove this
             api.StatusCodeShouldBeSuccess();    
        });

I considered throwing something if there are more than one assertion that is IStatusCodeAssertion, but that feels too heavy handed.

If this is accepted (and I can't help but think there might be a better way, or someone will see a problem with this), I'd be willing to update the documentation at https://jasperfx.github.io/alba/scenarios/assertions.html.

Thanks, and sorry?

JeffryGonzalez avatar May 16 '25 20:05 JeffryGonzalez

I'd say for now forget about this in light of the comment @jeremydmiller made about the changes to Wolverine default status codes (204, etc)

It seems changing Alba's default to "if you don't specify a status code it will check for 200" to "...it will check for 200-299" would be easier and not require what is in this PR.

JeffryGonzalez avatar May 18 '25 23:05 JeffryGonzalez

Will be fixed in v9

Hawxy avatar Jul 12 '26 05:07 Hawxy