Skip to content

Update Chapter 07 Integration Tests to use FluentAssertions.Web extensions #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

adrianiftode
Copy link

I noticed you are using FluentAssertions.Web for API testing, so I'm adding this PR to exemplify some of the usages in Chapter 07. FluentAsertions.Web does not only have nice extensions around the HttpResponse, but it also provides useful outputs when the tests are failing.

For example, if you would pass an invalid Bearer token, you would get the following message in the Test Detail Summary

 AuthenticationTests.Auth_ShouldGetPastAuthEndpointJWT
   Source: Integration.Tests.cs line 42
   Duration: 762 ms

  Message: 
Expected response to be HttpStatusCode.OK {value: 200}, but found HttpStatusCode.Unauthorized {value: 401}.

The HTTP response was:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token"


The originating HTTP request was:

GET http://localhost/api/Auth/testAuth HTTP 1.1
Authorization: Bearer invalid-token


  Stack Trace: 
XUnit2TestFramework.Throw(String message)
TestFrameworkProvider.Throw(String message)
DefaultAssertionStrategy.HandleFailure(String message)
AssertionScope.FailWith(Func`1 failReasonFunc)
AssertionScope.FailWith(Func`1 failReasonFunc)
AssertionScope.FailWith(String message, Object[] args)
HttpResponseMessageAssertions.Be200Ok(String because, Object[] becauseArgs)
HttpStatusCodeAssertionsExtensions.Be200Ok(HttpResponseMessageAssertions`1 parent, String because, Object[] becauseArgs)
AuthenticationTests.Auth_ShouldGetPastAuthEndpointJWT() line 50
--- End of stack trace from previous location ---

image

@lavedon
Copy link
Collaborator

lavedon commented Mar 27, 2025

Thanks bro! I do really like your library and we almost went with this but thought demonstrating only standard FluentAssertions and FluentAssertions.AspNetCore.Mvc was better for this chapter. That may have been a mistake and will re-think for next edition if we do. The isAuthenticated test is 🔥.

@lavedon lavedon closed this Mar 27, 2025
@adrianiftode
Copy link
Author

Cheers, good luck with the book!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants