我有一些Web Api Controller测试。这些都是使用.NET Core Tests提供的WebApplicationFactory构建的。
打印屏幕1 :全部成功
打印屏幕2 :全部失败,但以下一项除外:
所有测试都包含一个[Theory],该参数传入MemberData以测试各种安全组合。
subDoc
我在Dotnet运行时项目Github issue上找到了here。但是,我不确定是否是同一问题。另外,我似乎无法解决该问题。
有人对此有经验吗?我现在可以手动运行这些测试,但它也将是DevOps上CI管道的一部分。我想它也会在那里破裂。
非常感谢。
编辑:添加使用的代码
const names=[{ name: 'Apple' },{ name: 'Apricot' },{ name: 'Avocados' },{ name: 'Almond' },
{ name: 'Blueberries' }, { name: 'Bing Cherry' },{ name: 'Breadfruit' },{ name: 'Bananas' },
{ name: 'Cherries' },{ name: 'Custard-Apple' },{ name: 'Carrot' },{ name: 'Cranberries' },
{ name: 'Dates' }, { name: 'Dragon Fruit' },{ name: 'Durian' },
{ name: 'Grapefruit' },{ name: 'Grapes' },{ name: 'Gooseberries' },{ name: 'Guava' }]
const alphabets = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
];
const x = alphabets.map(x => ({
[x]: [...names.filter(name => name.name.startsWith(x)).map(m => m.name)]
}));
console.log(x);
编辑:CI结果
正如预期的那样,Azure DevOps CI管道也遇到此问题。以下是日志/结果:
7) Project.UserRolesControllerTests.GetUserDetailsById_ReturnsConfiguredResult
Duration: 1 ms
Message:
[Test Class Cleanup Failure (Project.Controllers.UserRolesControllerTests)]: System.ObjectDisposedException : The CancellationTokenSource has been disposed.
Stack Trace:
CancellationTokenSource.ThrowObjectDisposedException()
RedisStorage.Dispose()
Disposer.Dispose(Boolean disposing) line 38
Disposable.Dispose() line 32
LifetimeScope.Dispose(Boolean disposing) line 414
Disposable.Dispose() line 32
Container.Dispose(Boolean disposing) line 142
Disposable.Dispose() line 32
AutofacServiceProvider.Dispose() line 121
WebHost.Dispose()
WebApplicationFactory`1.Dispose(Boolean disposing)
WebApplicationFactory`1.Dispose()
编辑:程序包配置
public class MyCustomWebApplicationFactory<TStartup>
: WebApplicationFactory<TStartup> where TStartup : class
{
private UserConfiguration _userConfiguration;
public void SetUser(UserConfiguration userConfiguration)
{
_userConfiguration = userConfiguration;
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
Environment.SetEnvironmentVariable("Statistics:RedisDb", "<conn. string>");
builder.ConfigureTestServices(services =>
{
services.AddMvc(options =>
{
options.Filters.Add(new AllowAnonymousFilter()); // Remove entire JWT-requirement for all controllers during test
});
services.AddScoped<IJwtService>(instance =>
{
var customJwtService = new CustomJwtService(() => _userConfiguration);
return customJwtService;
});
services.AddScoped<IHttpRequestHeaderService>(instance =>
new CustomHttpRequestHeaderService(() => _userConfiguration));
});
builder.UseEnvironment("Test");
}
}
public class CommunityPostsControllerTests : IClassFixture<MyCustomWebApplicationFactory<CommunityPostsController>>
{
private MyCustomWebApplicationFactory<CommunityPostsController> _factory;
public CommunityPostsControllerTests(MyCustomWebApplicationFactory<CommunityPostsController> factory)
{
_factory = factory;
}
public static IEnumerable<object[]> RoleAccess => new List<object[]>
{
new object[] { UserConfigurations.AdminCi1, Ci1EmployeePostInGroup, HttpStatusCode.OK},
new object[] { UserConfigurations.EmployeeCi1, Ci1ParentPostInGroup, HttpStatusCode.Forbidden},
// Omitted for readability
};
[Theory]
[MemberData(nameof(RoleAccess))]
public async Task GetPostById_ReturnsConfiguredResult(UserConfiguration userConfiguration, int communityPostId, HttpStatusCode expectedCode)
{
// Arrange
_factory.SetUser(userConfiguration);
var client = _factory.CreateClient();
// Act
var result = await client.GetAsync($"api/v1/communityposts/{communityPostId}", cancellationToken: CancellationToken.None);
// Assert
Assert.Equal(expectedCode, result.StatusCode);
}
}