Skip to content

Instantly share code, notes, and snippets.

@oguzhancagliyan
Created September 30, 2020 00:07
Show Gist options
  • Save oguzhancagliyan/9563a68273ec8a44671013d160d6a947 to your computer and use it in GitHub Desktop.
Save oguzhancagliyan/9563a68273ec8a44671013d160d6a947 to your computer and use it in GitHub Desktop.
fixture for testserver
public class TestServerFixture : WebApplicationFactory<Startup>
{
private const string DynamoDbServiceUrl = "http://localhost:4566/";
private const string SqsServiceUrl = "http://localhost:4566/";
protected override IHostBuilder CreateHostBuilder()
{
var hostBuilder = base.CreateHostBuilder()
.UseEnvironment("Testing")
.ConfigureAppConfiguration(builder =>
{
var configPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
builder.AddJsonFile(configPath);
});
return hostBuilder;
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(collection =>
{
collection.Remove(new ServiceDescriptor(typeof(IDynamoDBContext),
a => a.GetService(typeof(IDynamoDBContext)), ServiceLifetime.Scoped));
AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig
{
RegionEndpoint = RegionEndpoint.EUCentral1,
UseHttp = true,
ServiceURL = DynamoDbServiceUrl
};
var dynamoDbClient = new AmazonDynamoDBClient("123", "123", clientConfig);
collection.AddScoped<IDynamoDBContext, DynamoDBContext>(opt =>
{
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
client = dynamoDbClient;
var dynamoDbContext = new DynamoDBContext(client);
return dynamoDbContext;
});
collection.Remove(new ServiceDescriptor(typeof(IAmazonDynamoDB),
a => a.GetService(typeof(IAmazonDynamoDB)), ServiceLifetime.Scoped));
collection.AddAWSService<IAmazonDynamoDB>(options: new AWSOptions
{
Region = RegionEndpoint.EUCentral1,
Credentials = new BasicAWSCredentials("123", "123"),
}, ServiceLifetime.Scoped);
collection.Remove(new ServiceDescriptor(typeof(IAmazonSQS), a => a.GetService(typeof(IAmazonSQS)),
ServiceLifetime.Scoped));
AmazonSQSConfig sqsConfig = new AmazonSQSConfig
{
RegionEndpoint = RegionEndpoint.EUCentral1,
UseHttp = true,
ServiceURL = SqsServiceUrl,
};
var sqsClient = new AmazonSQSClient("123", "123", sqsConfig);
collection.AddScoped<IAmazonSQS, AmazonSQSClient>(opt =>
{
AmazonSQSConfig localSqsConfig = new AmazonSQSConfig
{
RegionEndpoint = RegionEndpoint.EUCentral1,
UseHttp = true,
ServiceURL = SqsServiceUrl,
};
var localSqsClient = new AmazonSQSClient("123", "123", localSqsConfig);
return localSqsClient;
});
collection.RemoveAll(typeof(IHostedService));
DynamoDbSeeder.CreateTable(dynamoDbClient);
DynamoDbSeeder.Seed(dynamoDbClient);
SqsSeeder.CreateQueue(sqsClient);
});
base.ConfigureWebHost(builder);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment