Redesign for MongoDbFixture.cs

This commit is contained in:
Denis-Cosmin Nutiu 2021-05-30 20:39:04 +03:00
parent fd6da9c589
commit 7daa314ad1
3 changed files with 11 additions and 23 deletions

View file

@ -1,30 +1,32 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.Xunit2;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures;
using Retroactiune.Models;
using Retroactiune.Settings;
using Xunit;
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
{
public class TestFeedbackReceiver : IClassFixture<WebApiTestingFactory>, IClassFixture<MongoDbFixture>
public class TestFeedbackReceiver : IClassFixture<WebApiTestingFactory>
{
private readonly MongoDbFixture _mongoDb;
private readonly HttpClient _client;
public TestFeedbackReceiver(WebApiTestingFactory factory, MongoDbFixture mongoDbFixture)
public TestFeedbackReceiver(WebApiTestingFactory factory)
{
_client = factory.CreateClient();
_mongoDb = mongoDbFixture;
var dbSettings = factory.Services.GetService<IOptions<RetroactiuneDbSettings>>();
_mongoDb = new MongoDbFixture(dbSettings);
}

View file

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Retroactiune.Models;
using Retroactiune.Settings;
@ -16,21 +16,9 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures
public IMongoCollection<FeedbackReceiver> FeedbackReceiverCollection =>
Database.GetCollection<FeedbackReceiver>(_settings.FeedbackReceiverCollectionName);
public MongoDbFixture()
public MongoDbFixture(IOptions<RetroactiuneDbSettings> options)
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.Testing.json").Build();
var section = config.GetSection(nameof(RetroactiuneDbSettings));
_settings = new RetroactiuneDbSettings();
_settings.ConnectionString = section.GetValue<string>("ConnectionString");
_settings.TokensCollectionName = section.GetValue<string>("TokensCollectionName");
_settings.FeedbackCollectionName = section.GetValue<string>("FeedbackCollectionName");
_settings.FeedbackReceiverCollectionName = section.GetValue<string>("FeedbackReceiverCollectionName");
_settings.DatabaseName = section.GetValue<string>("DatabaseName");
_settings = options.Value;
var client = new MongoClient(_settings.ConnectionString);
Database = client.GetDatabase(_settings.DatabaseName);
}

View file

@ -1,7 +1,5 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
namespace Retroactiune
{