Skip to content

Testing

moattarwork edited this page Dec 14, 2022 · 1 revision

LittleBlocks offers some tools and libraries to facilitate the unit and integration testing. Using AutoFixture as baseline library, it enables the developers to put minimum efforts on creating the fake services and data and spend more time on testing the logic of the code. It mainly uses the following libraries:

Unit Testing

The unit testing library and extensions offer helpers to simplify the generation of Fake objects and services when the developer uses xUnit's TheoryAttribute.

To be able to use the unit testing helpers, the developer needs to install the following library from NuGet:

dotnet add package LittleBlocks.Testing

The package offers the following facilities:

[AutoSubstituteAndData]

The attribute combined with Theory will help on auto-generating the data and dependencies. Here is an example:

[Theory]
[AutoSubstituteAndData]
public void ShouldGenerateAutoDataAndSutWithDependency(string data, [Frozen] IMyDependency myDependency, MyService sut)
{
    // Arrange
    // Act
    sut.DoWork();
    // Assert
    myDependency.Received(1).DoWork();
    Assert.NotEmpty(data);
}

[AutoSubstituteAndInlineData]

The attribute is similar to AutoSubstituteAndData but it offers inline data as the parameter too. It will be helpful in the scenarios which alongside of the Fake data generation, the developer needs to specify some inputs. Here is an example which the first parameter (value) is passed to the attribute and the rest of the parameter, including sut is being generated using AutoFixture:

[Theory]
[AutoSubstituteAndInlineData("value1")]
public void ShouldGenerateAutoDataAndManualDataAndDependency(string value, string data, IMyDependency myDependency)
{
    // Arrange
    // Act
    // Assert
    Assert.False(string.IsNullOrEmpty(data));
    Assert.IsAssignableFrom<IMyService>(myDependency);
    Assert.Equal("value1", value);
}

FixtureBase

Alongside the auto-generation extensions for xUnit, The FixtureBase base class offers some facilities to generate Fake data and dependencies. It will isolate the developer code from the underlying framework and make it easier to change. The base case offers:

public ILogger<T> Logger<T>()
public T Fake<T>() where T : class
public T FakeEntity<T>()
public List<T> FakeEntityList<T>(int count = 3)
protected static T LoadSampleData<T>(string path) 

Integration Testing

The library offers helpers to set up integration tests, define real and fake services and define the behavior of an API under the test. It is especially helpful when the developer tries to test the API through the API client.

To be able to use the unit testing helpers, the developer needs to install the following library from NuGet:

dotnet add package LittleBlocks.Testing.Integration

Here is a sample:

public class ControllerFixture : IntegrationFixtureBase 
{

}


public class ControllerTests : IClassFixture<ControllerFixture>
{
	private readonly ControllerFixture _fixture;

	public ControllerTests(ControllerFixture fixture)
	{
		_fixture = fixture;
	}
	
	
	[Fact]
	public async Task
		Should_GetDataAsync_ReturnTheRigthSetOfData_WhenTheDependenciesAreCorrect()
	{
		// Arrange
		var client = _fixture.CreateClientFromServer<IAggregationClient>(s =>
		{
			s.GetRequiredService<IMappingsClient>()
			 .GetMappingsAsync()
			 .Returns(Task.FromResult(new MappingDto[] { }.AsEnumerable()));
		});

		// Act
		var actual = await client.GetDataAsync(_fixture.FundGroupId, _fixture.RequestedDate);

		// Assert
		actual.Should().NotBeNull();
		actual.Categories.Should().HaveCount(6);
		actual.Rows.Should().HaveCount(7);
	}
}
Clone this wiki locally