https://github.com/lucasteles/aspnet.configurationfaker
Easily change aspnet core configurations on your integration tests
https://github.com/lucasteles/aspnet.configurationfaker
Last synced: 3 months ago
JSON representation
Easily change aspnet core configurations on your integration tests
- Host: GitHub
- URL: https://github.com/lucasteles/aspnet.configurationfaker
- Owner: lucasteles
- License: mit
- Created: 2023-05-11T15:52:52.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-09-28T16:47:18.000Z (about 2 years ago)
- Last Synced: 2024-04-27T06:21:24.497Z (over 1 year ago)
- Language: C#
- Size: 29.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/lucasteles/AspNet.ConfigurationFaker/actions/workflows/ci.yml)
[](https://www.nuget.org/packages/AspNet.ConfigurationFaker)





# AspNet.ConfigurationFaker
Simple change AspNet Core configuration on integration tests
## Getting started
[NuGet package](https://www.nuget.org/packages/AspNet.ConfigurationFaker) available:
```ps
$ dotnet add package AspNet.ConfigurationFaker
```## How To Use:
```csharp
class TestFixture : WebApplicationFactory
{
FakeConfigurationProvider FakeConfig { get; } = new();protected override void ConfigureWebHost(IWebHostBuilder builder) => builder
.UseFakeConfigurationProvider(FakeConfig) // for startup/program config mock
.ConfigureAppConfiguration(config =>
{
config.RemoveJsonSource("appsettings.Local.json");config.AddFakeConfiguration(FakeConfig);
FakeConfig.ReplaceConfigurationUrls(config, "wiremock", "http://localhost:1234");
// replaces any "http://wiremock" value on test config for http://localhost:1234})
.ConfigureTestServices(services =>
{
/* ... */
});[OneTimeSetUp]
public void OneTimeSetup()
{
FakeConfig.Add("ApiKey", Guid.NewGuid());
FakeConfig.Freeze();
}[SetUp]
public void Setup() =>
FakeConfig.Reset(); // resets to the last frozen state[Test]
public void ConfigTest()
{
FakeConfig.Add("Parent:ChildValue", 42);FakeConfig.AddJson(
"""
{
"Parent": {
"ChildValue": 42
}
}
""");FakeConfig.AddAsJson(new
{
Parent = new
{
ChildValue = 42,
},
});/* Code that injects IConfiguration or IOptions */
}
}
);
```