Skip to content

Latest commit

 

History

History
94 lines (74 loc) · 3.48 KB

Readme.en.md

File metadata and controls

94 lines (74 loc) · 3.48 KB

Build status (DotNet.Core.Runtime.Faker)

Package Version 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)

DotNet.Core.Runtime.Faker

Lib to change runtime implementation by DI in integration tests

Installation

Using Moq

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

Using FakeItEasy

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

Configuration

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

var factory = new WebApplicationFactory<Startup>().WithWebHostBuilder(builder =>
    builder.ConfigureTestServices(services =>
    {
        services.AddServiceWithFaker<Clock>(faker => A.CallTo(() => faker.Now()).Returns(registeredValue));
    }));
var serviceProvider = factory.Services;
  • Change implementation
serviceProvider.Change<Clock>(faker => A.CallTo(() => faker.Now()).Returns(new DateTime()));
  • Set new value
serviceProvider.GetService<Clock>().Now();

Should returns new value =)

  • Clean implementation to avoid problems in anothers tests
serviceProvider.ResetAllChanges();

Using Moq

  • Lib DotNet.Core.Runtime.Faker.Moq

Very similar with FakeItEasy, but using moq syntax

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

and

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

Manually

  • Lib DotNet.Core.Runtime.Faker

Without any libs

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

and

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

Completed samples: