Skip to content

Lib para trocar implementações injetadas via DI em tempo de execução nos testes integrados

License

Notifications You must be signed in to change notification settings

Lambda3/DotNet.Core.Runtime.Faker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build status (DotNet.Core.Runtime.Faker)

Pacote Versão Downloads
Moq NuGet version (DotNet.Core.Runtime.Faker.Moq) NuGet version (DotNet.Core.Runtime.Faker.Moq)
FakeItEasy NuGet version (DotNet.Core.Runtime.Faker.FakeItEasy) NuGet version (DotNet.Core.Runtime.Faker.FakeItEasy)
Manual NuGet version (DotNet.Core.Runtime.Faker) NuGet version (DotNet.Core.Runtime.Faker)

DotNet.Core.Runtime.Faker

Read this in other language: English

Lib para trocar implementações injetadas via DI em tempo de execução nos testes integrados

Instalação

Com Moq

> dotnet add package Lambda3.DotNet.Core.Runtime.Faker.Moq

Com FakeItEasy

> dotnet add package Lambda3.DotNet.Core.Runtime.Faker.FakeItEasy

Manual

> dotnet add package Lambda3.DotNet.Core.Runtime.Faker

Configuração

Através do FakeItEasy

  • Lib DotNet.Core.Runtime.Faker.FakeItEasy
  • Registrar o faker
var registeredValue = new DateTime();

var factory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
    builder.ConfigureTestServices(services =>
    {
        services.AddServiceWithFaker<Clock>(faker => A.CallTo(() => faker.Now()).Returns(registeredValue));
    }));
var serviceProvider = factory.Services;
  • Mudar implementação
serviceProvider.Change<Clock>(faker => A.CallTo(() => faker.Now()).Returns(new DateTime()));
  • Receber novo valor
serviceProvider.GetService<Clock>().Now();

Deve retornar valor informado no change =)

  • Limpar implementação para não influenciar em outros testes
serviceProvider.ResetAllChanges();

Através do Moq

  • Lib DotNet.Core.Runtime.Faker.Moq

Muito parecido com o FakeItEasy, mas com a sintaxe do moq

factory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
builder.ConfigureTestServices(services =>
{
    services.AddServiceWithFaker<Clock>(mock => mock.Setup(x => x.Now()).Returns(registeredValue));
}));

e

 serviceProvider.Change<Clock>(mock => mock.Setup(x => x.Now()).Returns(new DateTime()));

Manualmente

  • Lib DotNet.Core.Runtime.Faker

Muito parecido com os anteriores, mas sem dependências das libs

factory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
builder.ConfigureTestServices(services =>
{
    services.AddServiceWithFaker<Clock>(() => new FakeClock());
}));

e

serviceProvider.Change<Clock>(() => new FakeClock());

Exemplos completos:

About

Lib para trocar implementações injetadas via DI em tempo de execução nos testes integrados

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages