Package | Version | Downloads |
---|---|---|
Moq | ||
FakeItEasy |
Lib to change runtime implementation by DI in integration tests
> dotnet add package Luna.DotNet.Core.Runtime.Faker.Moq
> dotnet add package Luna.DotNet.Core.Runtime.Faker.FakeItEasy
Using FakeItEasy
- 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()));
- 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: