Using xUnit to Test your C# Code

How to create unit tests and integration tests with xUnit for your C# applications

Brought to you by @andrea.chiarelli

Read on :open_book:

1 Like

Please, share your thoughts about the article and testing practices!

2 Likes

Previous post was deleted because of SPAM reasons.

Excellent article. Very exhaustive. Some small inputs, if I may :slightly_smiling_face:

To create the integration test project, move to the integration-tests folder, and type the following command:

There is no integration-tests in that repo. I created a new Test Project by adding it to the solution by opening it in VS2022

I had to change the below assert for the integration test method AddTermWithAuthorization. The second slash came back encoded?

Assert.Equal("/api/glossary%5CMFA", response.Headers.GetValues("Location").FirstOrDefault());

Also, how did the FakeJwtManager class know to add the Location header? Please if you can expand on that as well. I know its an old article. But if you get time, it would be awesome.

Again, thanks for the detailed explanation and code.

Hey @ruchinmunjal , thank you for appreciating my article. I hope it has been helpful for you.

Regarding the integration-test folder, a few paragraphs before, I invited the reader to create it:

So, to prepare your environment, move to the unit-integration-test-xunit folder, and create a new integration-tests folder.

Related to the encoded slash, it is weird. I can’t reproduce this issue. Also, it sounds strange that only that slash in the URL is encoded :thinking:

Finally, the FakeJwtManager class does not add the Location header. It simply generates fake access tokens instead of getting them from an authorization server such as Auth0.
The Location header is populated by the Web API. In particular, the POST method called on the api/glossary endpoint generates a response with the 201 Created status code and the URL of the newly created resource in the Location header, as per specifications (it’s one of the best practices in building APIs inspired by the REST paradigm).
You can take a look at the source code of the API endpoint to better understand it.

1 Like

This topic was automatically closed after 21 days. New replies are no longer allowed.

Excellent article, learned from it, thanks !

Just notice this assertion for response body:

var stringResponse = await response.Content.ReadAsStringAsync();

      Assert.Contains(terms, t => t.Term == "Access Token");
      Assert.Contains(terms, t => t.Term == "JWT");
      Assert.Contains(terms, t => t.Term == "OpenID");

there is Xunit json assertion package which can assert json so you can just directly assert this json body by:

string expectedJson = """
    [
      {"term": "Access Token"},
      {"term": "JWT"},
      {"term": "OpenID"}
    ]
    """;

JsonAssertion.Equivalent(expectedJson, await response.Content.ReadAsStringAsync());

As a reusable way :slight_smile:

1 Like

Hey @lateapexearlyspeed,
Thank you very much for your compliments and your feedback! :tada: