当用户点击真实服务时,c#单元测试失败了

时间:2016-09-10 06:52:20

标签: c# .net unit-testing nunit moq

所以我进行了单元测试,我发现不同的单元测试在nunit中失败,在Visual Studio中使用Resharper我尝试调试它并获得对象

1 2 3 4 

所以错误发生在这一行:

[Test] public void KeyDocumentService_ProofKeyDocument_RepoReturnsData_ServiceReturnsTheDataWithoutError() { //Arrange KeyDocumentProofRequest request = new KeyDocumentProofRequest() { KeyDocumentId = 2 }; string returnedResponse = "2"; KeyDocument keyDocumentResponse = new KeyDocument() { CampaignId = "2", DesignFileId = 3,DocumentId="2", DataSourceId="3", KeyDocumentId=1 }; List<vwKeyDocumentSearch> keyListResponse = new List<vwKeyDocumentSearch>() { new vwKeyDocumentSearch { FieldName = "test", FieldValue = "testvalue" } }; var uproduceRepo = new Mock<IUProduceRepository>(); var keyDocRepo = new Mock<IKeyDocumentRepository>(); var templateRepo = new Mock<ITemplateRepository>(); keyDocRepo.Setup(p => p.GetKeyDocument(It.IsAny<KeyDocumentRequest>())).Returns(new KeyDocumentResponse() { data = keyDocumentResponse }); keyDocRepo.Setup(p => p.GetKeyDocumentItems(It.IsAny<int>())).Returns(keyListResponse); uproduceRepo.Setup(p => p.ProduceDocument(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Customization[]>(), It.IsAny<string>(), It.IsAny<string>(), null)).Returns(returnedResponse); // Act. KeyDocumentService svc = new KeyDocumentService(keyDocRepo.Object, uproduceRepo.Object, templateRepo.Object); var response = svc.ProofKeyDocument(request); //Assert Assert.IsNotNull(response); Assert.IsNotNull(response.data.JobId); Assert.IsNull(response.Error); }

单元测试是否应该进入实际服务?还是那样吗?

方法var response = svc.ProofKeyDocument(request);看起来像这个FYI

ProofKeyDocument

我看到调试它在这条线上爆炸了 private List<Customization> GetCustomizationsFromKeyDocumentItems(List<vwKeyDocumentSearch> keyDocumentItemsList, int templateId, int clientId) { try { List<Customization> KeyDocumentCustomizations = new List<Customization>(); var keyDocumentVariableList = keyDocumentItemsList.Where(k => k.Type.ToUpper()=="VARIABLE").ToList(); var keyDocumentSettingList = keyDocumentItemsList.Where(k => k.Type.ToUpper() == "SETTING").ToList(); var keyDocumentContentList = keyDocumentItemsList.Where(k => k.Type.ToUpper() == "CONTENT").ToList(); KeyDocumentCustomizations.AddRange(VariableCustomizations(keyDocumentVariableList, templateId)); KeyDocumentCustomizations.AddRange(SettingCustomizations(keyDocumentSettingList, templateId)); KeyDocumentCustomizations.AddRange(ContentCustomizations(keyDocumentContentList, templateId, clientId)); return KeyDocumentCustomizations; } catch (Exception ex) { logger.Error(string.Format("Error customizing key document: {0}", templateId), ex); throw ex; } }

对象参考未设置为实例...错误为什么?

var keyDocumentVariableList = keyDocumentItemsList.Where(k => k.Type.ToUpper()=="VARIABLE").ToList();

 var keyDocumentResponse = _repo.GetKeyDocument(new KeyDocumentRequest() { KeyDocumentId = request.KeyDocumentId });

然后

  Customization[] customizations = GenerateCustomizationsForKeyDocument(keyDocumentDetails.KeyDocumentId, keyDocumentResponse);

1 个答案:

答案 0 :(得分:0)

已发布的测试不包含“CreteJobTicket”的模拟方法,而jobTickedId将为null。

var jobTicketId = _uproduceRepo.CreateJobTicket(keyDocumentDetails.DocumentId, keyDocumentDetails.DataSourceId, "PROOF");