Skip to content

Instantly share code, notes, and snippets.

[Binding]
public class WeatherHooks
{
private readonly IObjectContainer _objectContainer;
private const string AppSettingsFile = "appsettings.json";
public WeatherHooks(IObjectContainer objectContainer)
{
_objectContainer = objectContainer;
}
private async Task ClearData(
WebApplicationFactory<Program> factory)
{
if (factory.Services.GetService(typeof(IWeatherRepository))
is not IWeatherRepository repository) return;
var entities = await repository.GetAsync();
foreach (var entity in entities)
await repository.RemoveAsync(entity.Id);
}
private WebApplicationFactory<Program> GetWebApplicationFactory() =>
new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
IConfigurationSection? configSection = null;
builder.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), AppSettingsFile));
configSection = context.Configuration.GetSection(nameof(WeatherDatabaseSettings));
});
{
"WeatherDatabaseSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "WeathersTestDb",
"CollectionName": "Weathers"
}
}
{
"id": 1,
"date": "2022-01-01T06:00:00Z",
"temperatureC": 32,
"temperatureF": 89,
"summary": "Freezing",
"eTag": "6e9eff61-a3ed-4339-93fb-24151149b46c"
}
[Binding]
public class WeatherWebApiStepDefinitions
{
private const string BaseAddress = "http://localhost/";
public WebApplicationFactory<Program> Factory { get; }
public IWeatherRepository Repository { get; }
public HttpClient Client { get; set; } = null!;
private HttpResponseMessage Response { get; set; } = null!;
public JsonFilesRepository JsonFilesRepo { get; }
private WeatherForecast? Entity { get; set; }
public class JsonFilesRepository
{
private const string Root = "../../../json/";
public Dictionary<string, string> Files { get; } = new();
public JsonFilesRepository(params string[] files)
{
var filesList = files.ToList();
if (!filesList.Any())
foreach (var file in Directory.GetFiles(Root))
<ItemGroup>
<InternalsVisibleTo Include="SpecFlowWebApi.Specs" />
</ItemGroup>
public partial class Program { }
public interface IWeatherRepository
{
Task<IEnumerable<WeatherForecast>> GetAsync();
Task<WeatherForecast?> GetAsync(int id);
Task<WeatherForecast?> AddAsync(WeatherForecast entity);
Task<WeatherForecast?> UpdateAsync(WeatherForecast entity);
Task<int> RemoveAsync(int id);
}