我刚刚从现有应用程序中发现了一些现有代码,我试图理解单元测试中的前两行。这是单元测试:
[TestMethod]
public void CanDisplayReportWithPage()
{
var claimComponent = new Mock<IClaimsComponent>();
GlobalContainer.Unity.RegisterInstance(claimComponent.Object);
_pharmacyCdmService
.Setup(service => service.ReadAllTenantEstablishments((It.IsAny<Guid>())))
.Returns(ResponseHelper.CreateResponse(new TenantEstablishmentDataContract[0]));
_pharmacyCdmService
.Setup(service => service.ReadTenantById((It.IsAny<long>())))
.Returns(ResponseHelper.CreateResponse(new TenantDataContract()));
claimComponent.Setup(
component => component.FileDetailsClaimReport(It.IsAny<RamqDetailsClaimReportRequestDataContract>()))
.Returns(new RamqDetailsClaimReportResponseDataContract());
var result = (ViewResult)_controller.View(new DetailsReportModel(), 3);
_pharmacyCdmService.VerifyAll();
_claimIdentityHelper.VerifyAll();
Assert.IsNotNull(result);
Assert.IsNotNull(result.Model);
Assert.AreEqual("Index", result.ViewName);
Assert.IsInstanceOfType(result.Model, typeof(DetailsReportModel));
Assert.AreEqual(2, ((DetailsReportModel) result.Model).DataContract.PageTransaction);
}
此测试使用 Moq 和 Unity 的组合。我想了解的是为什么模拟实例(claimComponent)必须在 Unity 中注册。