WebAppFactory

master
bicijinlian 4 years ago
parent 3fa852bfb6
commit 61f18bcfd4

@ -0,0 +1,58 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XUnitDIStudy.Test.IntegrationTest
{
/// <summary>
/// 自定义WebApp工厂
/// </summary>
public class CustomWebAppFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override IHostBuilder CreateHostBuilder()
{
return base
.CreateHostBuilder()
.ConfigureHostConfiguration(config =>
{
config.AddEnvironmentVariables("ASPNETCORE");
});
}
protected override IWebHostBuilder CreateWebHostBuilder()
{
return base
.CreateWebHostBuilder()
.UseEnvironment("Testing");
}
protected override TestServer CreateServer(IWebHostBuilder builder)
{
return base.CreateServer(builder);
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
//Startup.ConfigureServices之后执行
builder.ConfigureServices(services =>
{
});
//后执行
builder.ConfigureTestServices(services =>
{
});
}
}
}

@ -0,0 +1,45 @@
using Microsoft.AspNetCore.Mvc.Testing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Extensions;
using Xunit.Sdk;
using XUnitDIStudy.Model;
namespace XUnitDIStudy.Test.IntegrationTest
{
[Collection(TestConst.IntegrationCollectionDefinitionKey)]
public class DefaultWebApplicationFactoryTest
{
private readonly WebApplicationFactory<WebApp.Startup> _factory;
public DefaultWebApplicationFactoryTest(WebApplicationFactory<WebApp.Startup> factory)
{
_factory = factory;
}
[Fact]
public async Task TestAsync()
{
// Arrange
var client = _factory.CreateClient();
// Act
var response = await client.GetAsync("/Default/GetAll");
// Assert
response.EnsureSuccessStatusCode(); // Status Code 200-299
List<Student> result = System.Text.Json.JsonSerializer.Deserialize<List<Student>>(response.Content.ReadAsStringAsync().Result);
Assert.NotNull(result);
Assert.True(result.Count > 0);
}
}
}

@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc.Testing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Extensions;
using Xunit.Sdk;
namespace XUnitDIStudy.Test.IntegrationTest
{
[CollectionDefinition(TestConst.IntegrationCollectionDefinitionKey)]
public class IntegrationSetup:ICollectionFixture<WebApplicationFactory<WebApp.Startup>>
{
}
}

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XUnitDIStudy.Test.IntegrationTest
{
public class TestServerManger
{
}
}

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XUnitDIStudy.Test
{
public class TestConst
{
public const string IntegrationCollectionDefinitionKey = "IntegrationTest";
}
}
Loading…
Cancel
Save