Skip to content

cangulo-nugets/cangulo.common.testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cangulo.common.testing

Goals

Main Goal:

  • Provide common attributes and methods to xunit tests

How to use cangulo.common.testing

Injecting and configuring a Interface that is used in a constructor

[Theory]
[AutoNSubstituteData]
public void InjectInterface(ICarService carService, int carId)
{

    // Arrange
    var expectedCar = CarsContants.OnlyCarAvailable;
    
    // Setting the interface behaviour
    carService.Get(carId).Returns(expectedCar);

    // Using the interface injected
    var sut = new CarController(carService);

    // Act
    var result = sut.GetCar(carId);

    // Assert
    result.Should().Be(expectedCar);
    carService.Received(1).Get(carId);
}

Injecting a complex object

[Theory]
[AutoNSubstituteData]
public void InjectEntityClass(ICarService carService, Car expectedCar)
{

    // Arrange
    // using the complex object expectedCar
    carService.Get(expectedCar.CarId).Returns(expectedCar);

    var sut = new CarController(carService);

    // Act
    var result = sut.GetCar(expectedCar.CarId);

    // Assert
    result.Should().Be(expectedCar);
    carService.Received(1).Get(expectedCar.CarId);
}

Injecting the target class (subject under test) and provide the services (interfaces)

[Theory]
[AutoNSubstituteData]
public void InjectClassAndInteface(
    [Frozen] ICarService carService,    // service
    Car expectedCar,
    CarController sut)  // target class (subject under test)
{

    // Arrange
    carService.Get(expectedCar.CarId).Returns(expectedCar);

    // Act
    var result = sut.GetCar(expectedCar.CarId);

    // Assert
    result.Should().Be(expectedCar);
    carService.Received(1).Get(expectedCar.CarId);
}

Inspired by:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages