Skip to main content

Posts

Showing posts with the label xUnit

A Demo In .NET6 Mock HttpClient Using XUnit

In this article, we are going to test our HttpClient instance in .NET6 application by mocking it using the XUnit. Click here for .NET6 Web API Unit testing using XUnit that covers testing for 'Controllers', 'Service Logic', 'Entity framework Core Database Context'. Create An API And Unit Test Project: Let's create a .Net6 Web API and xUnit sample applications to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any.Net6 application. For this demo, I'm using the 'Visual Studio Code'(using the .NET CLI command) editor. Create a folder where we want to set up our applications. Then add a solution file by running the below command. Command To Add Solution File: dotnet new sln -n Name_of_your_solution_file Now let's create a .NET6 Web API project. dotnet new webapi -o Name_of_your_API_project Now let's create xUnit project. dotnet new xunit -o Name_of_your_API_project No

Unit Testing Asp.NetCore Web API Using xUnit[.NET6]

In this article, we are going to write test cases to an Asp.NetCore Web API(.NET6) application using the xUnit. xUnit For .NET: The xUnit for .Net is a free, open-source, community-focused unit testing tool for .NET applications. By default .Net also provides a xUnit project template to implement test cases. Unit test cases build upon the 'AAA' formula that means 'Arrange', 'Act' and 'Assert' Arrange - Declaring variables, objects, instantiating mocks, etc. Act - Calling or invoking the method that needs to be tested. Assert - The assert ensures that code behaves as expected means yielding expected output. Create An API And Unit Test Projects: Let's create a .Net6 Web API and xUnit sample applications to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any.Net6 application. For this demo, I'm using the 'Visual Studio Code'(using the .NET CLI command) editor. Create a fo