public void AddUpdateInvoice_Test() { #region Data int mrmUserId = 1; string networkLogin = "******"; InvoiceViewModel viewModel = new InvoiceViewModel() { Id = 0, Vendor = 142, PaymentTerm = 9, Status = 2, InvoiceNumber = "123", InvoiceDate = DateTime.Now, PostingDate = DateTime.Now, Comments = "Invoice Creation Comments", WebUser = 123,//mrmuserId, timestamp = null }; List <InvoiceLineViewModel> lstInvoiceLines = new List <InvoiceLineViewModel>(); InvoiceLineViewModel invoiceLine1 = new InvoiceLineViewModel() { ContractNumber = "1234", PurchaseOrderNumber = "Abcd1234", Description = "Test InvoiceLine1 to insert", Comment = "Test Comment to insert", Amount = 250, DeliverableBudgetId = 129289, GLAccountId = 50, Id = 1, ProductionMethodType = 2, WBSElementId = 3250, WBSNumber = "1146", Coding = "", ChannelCostCenterId = 21, ChannelProfitCenterId = null, }; InvoiceLineViewModel invoiceLine2 = new InvoiceLineViewModel() { ContractNumber = "2345", PurchaseOrderNumber = "Abcd2345", Description = "Test InvoiceLine2 to insert", Comment = "Test Comment to insert", Amount = 124, DeliverableBudgetId = 13458, GLAccountId = 50, Id = 1, ProductionMethodType = 1, WBSElementId = 3250, WBSNumber = "1146", Coding = "", ChannelCostCenterId = null, ChannelProfitCenterId = 21 }; Vendor vendor = new Vendor() { Name = "TestVendor1", Id = 1, }; InvoiceStatus status = new InvoiceStatus() { Id = 1, Name = "In Progress" }; lstInvoiceLines.Add(invoiceLine1); lstInvoiceLines.Add(invoiceLine2); viewModel.InvoiceLines = lstInvoiceLines; InvoiceHeader theInvoice = FinanceMapper.ToInvoice(viewModel); theInvoice.Vendor = vendor; theInvoice.InvoiceStatus = status; InvoiceHeader theInvoice1 = FinanceMapper.ToInvoice(viewModel); theInvoice1.Id = 999; #endregion #region Mock //invoiceHeaderRepository Mock mockInvoiceHeaderRepository.Setup(x => x.Add(It.IsAny <InvoiceHeader>())).Returns(theInvoice); //iunit Mock mockIUnitOfWork.Setup(x => x.Commit()); //IInvoiceLine Repository Mock mockIInvoiceLineRepository.Setup(x => x.Add(It.IsAny <InvoiceLine>())).Returns(() => null); //FinanceService Service Mock mockFinanceService.Setup(x => x.AddInvoice(It.IsAny <InvoiceHeader>())).Returns(theInvoice); mockFinanceService.Setup(x => x.GetInvoiceById(It.IsAny <int>())).Returns(theInvoice); this.mockInvoiceHeaderRepository.Setup(x => x.GetById(It.IsAny <long>())).Returns(theInvoice); this.mockInvoiceHeaderRepository.Setup(x => x.GetById(It.IsAny <long>())).Returns(theInvoice1); mockInvoiceHeaderRepository.Setup(x => x.GetInvoiceById(It.IsAny <int>())).Returns(theInvoice); mockIInvoiceLineRepository.Setup(x => x.GetMany(It.IsAny <Expression <Func <InvoiceLine, bool> > >())) .Returns(new List <InvoiceLine>()); mockIInvoiceLineRepository.Setup(x => x.GetById(It.IsAny <long>())) .Returns(new InvoiceLine()); mockInvoiceHeaderRepository.Setup(x => x.Update(It.IsAny <InvoiceHeader>())); mockIInvoiceLineRepository.Setup(x => x.Update(It.IsAny <InvoiceLine>())); //mock to finance Service FinanceServiceMock financeService = new FinanceServiceMock( invoiceHeaderRepository: mockInvoiceHeaderRepository.Object, unitOfWork: mockIUnitOfWork.Object, invoiceLineRepository: mockIInvoiceLineRepository.Object); //Finance Controller Mock var FinanceController = new FinanceControllerMock(financeServicee: financeService); #endregion #region Assertions mockInvoiceHeaderRepository.Verify(); mockIUnitOfWork.Verify(); mockIInvoiceLineRepository.Verify(); mockFinanceService.Verify(); //Mapping the viewmodel to actual Table var serviceResult = financeService.AddInvoice(theInvoice); Assert.IsNotNull(serviceResult); Assert.IsNotNull(serviceResult.InvoiceLine); Assert.AreEqual(serviceResult.Id, 0);//The actual Id is auto generated after insertion done in DB. //As we are passing 0 as invoice headerId for the purpose of insertion here we recieve the same. var controllerResult = FinanceController.AddUpdateInvoice(viewModel, mrmUserId, networkLogin); Assert.IsNotNull(controllerResult); Assert.AreEqual(controllerResult.Success, true); Assert.IsNotNull(controllerResult.Data); //Service N Controller Assertions for Updating Invoice viewModel.Id = 999; //Mapping the viewmodel to actual Table var serviceResultForUpdate = financeService.UpdateInvoice(theInvoice1); Assert.IsNotNull(serviceResultForUpdate); Assert.IsNotNull(serviceResultForUpdate.InvoiceLine); Assert.AreEqual(serviceResultForUpdate.Id, 999);//Here we will recieve the same invoice header id we passed for update var controllerUpdateResult = FinanceController.AddUpdateInvoice(viewModel, mrmUserId, networkLogin); Assert.IsNotNull(controllerUpdateResult); Assert.AreEqual(controllerUpdateResult.Success, true); Assert.IsNotNull(controllerUpdateResult.Data); #endregion }