Jul*_*ida 3 c# integration-testing nunit asp.net-core asp.net-core-webapi
我正在为Web api项目使用aspnetcore 3.0预览版7。目前,我正在实施集成测试。(为了使测试更容易,我在控制器上注释了Authorize属性。)
服务器响应“找不到404”。我对启动中不再有“ usemvc”感到困惑-我现在应该在设置测试服务器上做些不同的事情吗?还是有人知道导致404的原因?(用于3.0集成测试的MS文档尚未更新)。我也尝试了预览8,但同样的问题。
测试类别:
[OneTimeTearDown]
public virtual void Cleanup()
{
_unAuthenticatedServer.Dispose();
}
[OneTimeSetUp]
public virtual void Initialize()
{
_unAuthenticatedServer = CreateServer(null);
}
protected TestServer CreateServer(
string currentDirectory = null)
{
IWebHostBuilder webHostBuilder = WebHost.CreateDefaultBuilder();
webHostBuilder.UseContentRoot(currentDirectory == null
? Directory.GetCurrentDirectory()
: Directory.GetCurrentDirectory() + $"\\{currentDirectory}");
webHostBuilder.UseStartup<TestStartup>();
webHostBuilder.UseEnvironment("Test");
webHostBuilder.ConfigureAppConfiguration((_, config) => config.AddJsonFile("appsettings.Test.json"));
return new TestServer(webHostBuilder);
}
[Test, Order(1)]
public async Task Should_Get_Games_Return_StatusCode_Ok()
{
//Arrange
IList<GameDto> expectedDtos = GameDtoTestData.Games;
//Act
HttpResponseMessage responseMessage = await _unAuthenticatedServer
.CreateClient()
.GetAsync("api/games");
//Assert
responseMessage.StatusCode.Should().Be(HttpStatusCode.OK); // but is: 404 NotFound
var responseString = await responseMessage.Content.ReadAsStringAsync();
IEnumerable<GameDto> result = JsonConvert.DeserializeObject<IEnumerable<GameDto>>(responseString);
result.Should().BeEquivalentTo(expectedDtos);
}
Run Code Online (Sandbox Code Playgroud)
控制器:
[Route("api/[controller]")]
[ApiController]
public class GamesController : ControllerBase
{
private readonly IGameService _gameService;
public GamesController(IGameService gameService)
{
_gameService = gameService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<GameDto>>> Get()
{
return Ok(await _gameService.GetAsync());
}
}
Run Code Online (Sandbox Code Playgroud)
已安装Nuget软件包测试项目:
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.8.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.0.0-preview7.19365.7" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.0-preview7.19365.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.6" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.14.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
已安装Nuget包api项目:
<ItemGroup>
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureADB2C.UI" Version="3.0.0-preview6.19307.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
对我有用的是将包含Startup类的程序集注册为Mvc应用程序零件。
在启动类中,在Modify IServiceProvider ConfigureServices(IServiceCollection services)中使其包含以下代码:
services
.AddMvc(...)
.AddApplicationPart(typeof(Startup).Assembly)
Run Code Online (Sandbox Code Playgroud)
您需要将包含Startup类的程序集注册为Startup类本身中的应用程序部分,这似乎有些奇怪。但是这种方法确实对我有用。
| 归档时间: |
|
| 查看次数: |
371 次 |
| 最近记录: |