當單元測試時,系統無法解析UnitOfWorkManager.Current
(null參考異常)。 _unitOfWorkManager
的類型爲:IUnitOfWorkManager
,並通過DI注入。單元測試中嘲笑UnitOfWorkManager.Current
我測試了以下Web API代碼:
[HttpPost]
public async Task<IHttpActionResult> Subscription(SubscriptionInputDto model)
{
if (!await VendorHasAccess(model.OrganizationId))
return StatusCode(HttpStatusCode.Forbidden);
var input = new Subscription
{
...
};
var data = await _subscriptionRepository.InsertAsync(input);
await _unitOfWorkManager.Current.SaveChangesAsync();
return Ok(data.MapTo<SubscriptionDto>());
}
...用下面的單元測試:
[Fact]
public async Task Should_Create_New_Subscription()
{
//Arrange
var organization = await UsingDbContextAsync(async context => await context.VendorOrganizations.FirstOrDefaultAsync());
var input = new SubscriptionInputDto { OrganizationId = organization.Id };
//Act
var actionResult = await _vendorController.Subscription(input);
var response = actionResult as OkNegotiatedContentResult<SubscriptionDto>;
//Assert
Assert.NotNull(response);
Assert.Equal(organization.Id, response.Content.OrganizationId);
}
我想不通,或找到任何文件,如何解決UnitOfWorkManager.Current
,以便在單元測試中不會失敗。 Web API代碼本身在生產中工作。
任何提示?
你如何初始化'_vendorController'?它應該通過'iocManager'獲取,而不是通過'new VendorController'來獲取。 –
是的,像這樣:'_vendorController = Resolve();' –
Oakfire
您的單元測試類是否從'Abp'基礎設施的現有類繼承? –