Extending the test coverage 

As we did with the ItemRepositoryTests class, we can proceed by testing ArtistRepository and GenreRepository using the same approach. In Chapter 8Building the Data Access Layer, we defined TestDataContextFactory, which is part of the Catalog.Fixtures project. We can use this to instantiate an in-memory database for our test purposes:

using System;
using System.Linq;
using System.Threading.Tasks;
using Catalog.Domain.Entities;
using Catalog.Fixtures;
using Catalog.Infrastructure.Repositories;
using Shouldly;
using Xunit;

namespace Catalog.Infrastructure.Tests
{
public class ArtistRepositoryTests :
IClassFixture<CatalogContextFactory>
{
private readonly CatalogContextFactory _factory;

public ArtistRepositoryTests(CatalogContextFactory factory)
{
_factory = factory;
}

[Theory]
[LoadData("artist")]
public async Task should_return_record_by_id(Artist artist)
{
var sut = new ArtistRepository(_factory.ContextInstance);
var result = await sut.GetAsync(artist.ArtistId);

result.ArtistId.ShouldBe(artist.ArtistId);
result.ArtistName.ShouldBe(artist.ArtistName);
}

[Theory]
[LoadData("artist")]
public async Task should_add_new_item(Artist artist)
{
artist.ArtistId = Guid.NewGuid();
var sut = new ArtistRepository(_factory.ContextInstance);

sut.Add(artist);
await sut.UnitOfWork.SaveEntitiesAsync();

_factory.ContextInstance.Artist
.FirstOrDefault(x => x.ArtistId == artist.ArtistId)
.ShouldNotBeNull();
}
}
}

The previous code explores a way to implement the tests for the ArtistRepository class. The ArtistRepositoryTests class extends IClassFixture<CatalogContextFactory> to retrieve an instance of the CatalogContextFactory type. The test methods use the ContextInstance attribute to retrieve a new catalog context and to initialize a new repository.

They proceed by executing the method as a test and checking the results. It is important to notice that every test method uses the LoadData attribute in order to load the artist section of the record-data.json file. For brevity, I've omitted some of the test cases; however, the concept behind them is identical to what we have already seen, and it can be extended to the GenreRepository tests. 

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

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