Creating a web test project

Unit testing is the one that checks code health. This means that if the code is buggy (unhealthy), that would be the basis of many unknown and unwanted issues in the application. To overcome this approach, we could follow the TDD approach.

You can practice TDD with Katas. You can refer to https://www.codeproject.com/Articles/886492/Learning-Test-Driven-Development-with-TDD-Katas to find out more about TDD katas. If you want to practice this approach, use this repository: https://github.com/garora/TDD-Katas.

We have already discussed a lot about TDD in previous chapters, so we are not going to discuss this in detail here. Instead, let's create a test project as follows:

  1. Open our web application.
  2. From Solution Explorer in Visual Studio, right-click on Solution and click on Add | New Project..., as shown in the following screenshot:

  1. From the Add New Project template, select .NET Core and xUnit Test Project (.NET Core) and provide a meaningful name:

You will get a default unit test class with an empty test code, as shown in the following code snippet:

namespace Product_Test
{
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}
}

You can change the name of this class or discard this class if you want to write your own test class:

public class ProductData
{
public IEnumerable<ProductViewModel> GetProducts()
{
var productVm = new List<ProductViewModel>
{
new ProductViewModel
{
CategoryId = Guid.NewGuid(),
CategoryDescription = "Category Description",
CategoryName = "Category Name",
ProductDescription = "Product Description",
ProductId = Guid.NewGuid(),
ProductImage = "Image full path",
ProductName = "Product Name",
ProductPrice = 112M
},
...
};

return productVm;
}
  1. The previous code is from our newly added ProductDate class. Please add this to a new folder called Fake. This class just creates dummy data so that we can test our web application for the product:
public class ProductTests
{
[Fact]
public void Get_Returns_ActionResults()
{
// Arrange
var mockRepo = new Mock<IProductRepository>();
mockRepo.Setup(repo => repo.GetAll()).Returns(new ProductData().GetProductList());
var controller = new ProductController(mockRepo.Object);

// Act
var result = controller.GetList();

// Assert
var viewResult = Assert.IsType<OkObjectResult>(result);
var model = Assert.IsAssignableFrom<IEnumerable<ProductViewModel>>(viewResult.Value);
Assert.NotNull(model);
Assert.Equal(2, model.Count());
}
}
  1. Add a new file called ProductTests in the Services folder. Please note that we are using Stubs and Mocks in this code.

Our previous code will complain about the error using red squiggly lines, as shown in the following screenshot:

  1. The previous code has errors as we did not add certain packages that were required for us to perform tests. To overcome these errors, we should install moq support to our test project. Pass the following command in your Package Manager Console:
install-package moq 
  1. The preceding command will install the moq framework in the test project. Please note that while firing the preceding command, you should select the test project that we have created:

Once moq is installed, you can go ahead and start testing.

Important points to note while you're working with the xUnit test projects are as follows:
  • Fact is an attribute and is used for a normal test method that is without parameters.
  • Theory is an attribute and is used for a parameterized test method.
  1. All set. Now, click on Test explorer and run your tests:

Finally, our tests have passed! This means that our controller methods are good, and we do not have any issues or bugs in our code that can break the functionality of the application/system.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.55.14