Testing & Debugging

Testing and Debugging, ANGULAR WITH CORE .NET AND MVC RAZOR ENGINE

                                     Testing & Debugging


Here I'll brief some important points on debugging and why it is essential.as well as dive into the implementation details for creating unit and integration tests.

Why Is Testing Important?

Since from last few years, software testing has become an integral part of product quality assurance
(QA). We all do some level of testing(Manual or Automation) of our code before deploying it to a production
environment, be it a simple little quick-and-dirty exploratory testing or full-blown
automated testing that covers nearly 104 percent of the code.
One can imagine what would happen if car companies just designed, built, and
released their vehicles without first doing some form of testing. Software is no different.
Just like in the case of cars, users rely on working software to do their daily tasks.
In the context of software development, testing is the process of verifying if a
particular software component satisfies a specific functional requirement and a way to
gain insight into the test-result metrics to ensure that the quality of the software is on a
certain level.
To accelerate development turnaround times and to do testing consistently, we make
use of testing tools and frameworks that allow us to perform automated testing tasks
against structured scenarios. Test automation reduces the risk of human error because
humans suck at routine jobs, but machines thrive at them.

Software testing methods, such as unit testing and integration testing, are used
within testing frameworks to facilitate the verification process for a specific scenario. As
software development evolves, more sophisticated development processes arise, like
test-driven development (TDD) and behavior-driven development (BDD).
TDD is a process of developing software in the repetition of short iterations.
Functional requirements are transformed into specific test cases that should fail upon
initial execution. Just enough software is written to make a particular test case pass, then, if need be, the software is refactored to improve code quality. As a result of refactoring, some test cases might again fail, and then the whole process starts all over by either changing the test case or changing the software code. BDD is derived from TDD and combines the techniques and principles of TDD with the ideas of object-oriented design (OOD) and domain-driven design (DDD). The structure of test cases in BDD leans toward a user story that contains a title, a short narrative detailing who, what, and why, as well as acceptance criteria to validate if the requirement is satisfied. One of the main reasons why testing is necessary is not just the metrics it produces, but also the quality it ensures during the software development process. Test cases align with the code coverage of unit and integration tests to provide useful insights, whether tests cover all or only some of the scenarios.

Unit Testing

we will be exploring the implementation of unit tests using some of the
tools made available by Microsoft and the community. We will be building a solution
from scratch to learn the basics of creating unit tests in .NET Core.

Let's create  a new project (class library) using below command 

  1. dotnet new sln
  2. dotnet new classlib -n MyUnitTestCase
  3. dotnet sln add .\MyUnitTestCase\MyUnitTestCase.csproj

Note: we can delete class1.cs  file, as I'll be creating a new one

Open  above file in visual studio and have a look on below code

using System;
using System.Collections.Generic;
using System.Text;
 
namespace MyUnitTestCase
{
    interface IRandomNumber
    {
       int getRandomNumber();
    }
    class RandomNumber : IRandomNumber
    {
        public int getRandomNumber()
        {
            int randomNumber =  new Random().Next(1, int.MaxValue);
            Console.WriteLine("Random number is :" + randomNumber);
            return randomNumber;
        }
    }
}

Note: This is a class library so we won't be able to run. Since we are learning now Testing and Debugging so I'll majorly focus on testing of it (Automation)
Let's add XUnit project to automate it , there we will get our result using below command

  • dotnet new xunit -n MyUnitTestCase.Tests
  • dotnet sln add .\MyUnitTestCase.Tests\MyUnitTestCase.Tests.csproj
  • cd .\MyUnitTestCase.Tests
  • dotnet add reference ..\MyUnitTestCase\MyUnitTestCase.csproj


Since I'm using visual studio, so now it should look like below



 Modify the UnitTest.cs as below

using System;
using Xunit;
 

namespace MyUnitTestCase.Tests
{
    public class UnitTest1
    {
        [Fact]
        public void RandomNumberTestCase()
        {
            IRandomNumber randomNumber = new RandomNumber();
            int randumNumber = randomNumber.getRandomNumber();           
             int number = int.Parse(randumNumber.ToString());
            Assert.Equal(randumNumber, number);
        }
    }
}

Use Below command to run this test case

  • dotnet test

It should look like below 

Dealing with Dependencies

Unit testing is focused on testing single units of work. Methods that are being tested may
contain references to other dependencies, however, and if those dependencies are also
encapsulated in the method during unit testing, it is not testing a single unit anymore.

Have a look on below code


using System.Collections.Generic;
using System.Text;
 
namespace MyUnitTestCase
{
   public interface IRandomNumber
    {
       int getRandomNumber();
    }
    public class RandomNumber  : IRandomNumber
    {       
        public int getRandomNumber()
        {
            int randomNumber =  new Random().Next(1, int.MaxValue);
            Console.WriteLine("Random number is :" + randomNumber);
            return randomNumber;
        }
    }
 
    public class RandomService
    {
        private readonly IRandomNumber randomNumber;
        public RandomService(IRandomNumber randomNumber) // DI
        {
            this.randomNumber = randomNumber;
        }
 
        public int getRandumNumber()
        {
            int number = this.randomNumber.getRandomNumber();
            return number;
        }
    }
}

 

Use below command to add a new package

  • dotnet add package Moq

have a look on below code

  [Fact]
        public void getRandomNumberTestCase()
        {
            var personRepositoryMock = new Mock<IRandomNumber>();
            personRepositoryMock.Setup(p => p.getRandomNumber());      
            RandomService personService = new RandomService(personRepositoryMock.
            Object);
            var actualResult = personService.getRandumNumber();
            var expectedResult = actualResult;
            Assert.Equal(expectedResult, actualResult);
        }


Try to run your test case again. it will start working

With ❤️ Happy Coding

Comments

Popular posts from this blog

How to disable table row in Angular

How to create XML file dynamically in c#.net?

How to get Array value in Angular ?