# Using xUnit to Test your C# Code

**URL:** <https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657>\
**Category:** Blog Discussions\
**Tags:** dotnet-core, dotnet, unit-testing\
**Created:** [April 7, 2020, 2:47pm UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657 "2020-04-07T14:47:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bachi.degli-innocent](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bachi.degli-innocent/32/16949_2.png) [@bachi.degli-innocent](https://community.auth0.com/u/bachi.degli-innocent)\
**Post date:** [April 7, 2020, 2:47pm UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657/1 "2020-04-07T14:47:34Z")

</div>

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

Brought to you by @andrea.chiarelli

[Read on 📖](https://auth0.com/blog/xunit-to-test-csharp-code/)

 ![Using xUnit to Test your C# Code](https://global.discourse-cdn.com/auth0/original/3X/3/f/3ff8e7ac1ba097df1ccfc384254aba421a9913b2.jpeg)

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [April 7, 2020, 4:04pm UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657/2 "2020-04-07T16:04:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [June 19, 2020, 6:47am UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657/5 "2020-06-19T06:47:19Z")

</div>

Previous post was deleted because of SPAM reasons.

---

<div class="post-metadata">

**Author:** ![ruchinmunjal](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ruchinmunjal/32/32774_2.png) [@ruchinmunjal](https://community.auth0.com/u/ruchinmunjal)\
**Post date:** [November 3, 2021, 2:47pm UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657/6 "2021-11-03T14:47:31Z")

</div>

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

> 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.

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [November 4, 2021, 10:03am UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657/7 "2021-11-04T10:03:32Z")

</div>

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 🤔

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](https://httpwg.org/specs/rfc7231.html#status.201) (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](https://github.com/auth0-blog/glossary-web-api-aspnet-core/blob/04f9233ef6a0334f17fb032f8721b80852f21b0e/Controllers/GlossaryController.cs#L70) to better understand it.

---

<div class="post-metadata">

**Author:** ![robertino.calcaterra](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robertino.calcaterra/32/66825_2.png) [@robertino.calcaterra](https://community.auth0.com/u/robertino.calcaterra)\
**Post date:** [December 1, 2021, 4:00pm UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657/8 "2021-12-01T16:00:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 15, 2021, 3:58pm UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657/9 "2021-12-15T15:58:04Z")

</div>



---

<div class="post-metadata">

**Author:** ![lateapexearlyspeed](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lateapexearlyspeed/32/55515_2.png) [@lateapexearlyspeed](https://community.auth0.com/u/lateapexearlyspeed)\
**Post date:** [March 10, 2024, 6:58am UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657/10 "2024-03-10T06:58:34Z")

</div>

Excellent article, learned from it, thanks !

Just notice this assertion for response body:

```csharp
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](https://github.com/lateapexearlyspeed/Lateapexearlyspeed.JsonSchema) which can assert json so you can just directly assert this json body by:

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

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

```

As a reusable way 🙂

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [March 11, 2024, 9:52am UTC](https://community.auth0.com/t/using-xunit-to-test-your-c-code/40657/11 "2024-03-11T09:52:33Z")

</div>

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