最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Verifying multiple POST payloads in a unit test - Stack Overflow

programmeradmin0浏览0评论

I have a unit test that currently posts an array of order lines to a third party vendor. They have requested that I change the code to send each order line as a single JSON payload. Making the code change was easy enough, but I'm having an issue trying to update the unit test to validate the JSON for the first two order lines.

In the code below, the inputFile has the array of order lines and outputFile1 and outputFile2 are the first two order lines, formatted to the DTO JSON.

 [TestMethod]
 [DataRow(@"/sample-data/2-return-no-receipt-dto.json", @"/sample-data/2-return-no-receipt-Line1.json", @"/sample-data/2-return-no-receipt-Line2.json", "")]
 [DataRow(@"/sample-data/3-sale-dto.json", @"/sample-data/3-sale-Line1.json", @"/sample-data/3-sale-Line2.json", "")]
 [DataRow(@"/sample-data/4-return-dto.json", @"/sample-data/4-return-Line1.json", @"/sample-data/4-return-Line1.json", @"/sample-data/4-return-orig-dto.json")]
 [DataRow(@"/sample-data/1-retailsales-csc-dto.json", @"/sample-data/1-retailsales-Line1.json", "", "")]
 public async Task SuccessAndCompleteMessage(string inputFile, string outputFile1, string outputFile2, string origOrderFile)
 {
     //Arrange
     var inputFileContent = File.ReadAllText($@"{Environment.CurrentDirectory}{inputFile}");
     var saleDto = JsonConvert.DeserializeObject<SaleDto>(inputFileContent);

     var outputFileContent1 = File.ReadAllText($@"{Environment.CurrentDirectory}{outputFile1}");
     var outputFileContent2 = File.ReadAllText($@"{Environment.CurrentDirectory}{outputFile2}");

     var lockToken = Guid.NewGuid().ToString();
     var fixture = new Fixture();
     var msg = new ServiceBusRetailSalesMessage();
     msg.CallbackUrl = ";store=61";

     var testMessage = ServiceBusModelFactory.ServiceBusReceivedMessage(new BinaryData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg))));

     var response = new HttpResponseMessage();
     response.StatusCode = System.Net.HttpStatusCode.OK;
     response.Content = new StringContent(JsonConvert.SerializeObject(msg));

     _mockServiceBusMessageReceiver.Setup(x => x.ParseServiceBusMessage(It.IsAny<ServiceBusReceivedMessage>()))
         .Returns(msg);

     if (!origOrderFile.IsNullOrWhiteSpace())
     {
         var originalOrderContent = File.ReadAllText($@"{Environment.CurrentDirectory}{origOrderFile}");
         var originalOrder = JsonConvert.DeserializeObject<SaleDto>(originalOrderContent);

         _mockDtoHttpService.SetupSequence(x => x.GetDtoAsync<SaleDto>(It.IsAny<string>()))
             .Returns(Task.FromResult(saleDto))
             .Returns(Task.FromResult(originalOrder));
     }
     else
     {
         _mockDtoHttpService.Setup(x => x.GetDtoAsync<SaleDto>(It.IsAny<string>()))
            .Returns(Task.FromResult(saleDto));
     }

     _mockAdapterAuthHttpService.Setup(x => x.GetCurrentWriteTokenAsync(It.IsAny<string>()))
         .Returns(Task.FromResult(response));
     _mockAdapterAuthHttpService.Setup(x => x.PostNewTokenRequest(It.IsAny<NewTokenRequest>(), It.IsAny<string>()))
         .Returns(Task.FromResult(response));

     _mockAdapterService.Setup(x => x.PostAsync(It.IsAny<List<TransactionDto>>(), It.IsAny<string>()))
         .Returns(Task.FromResult(response));

     _mockAdapterService.Setup(x => x.GenerateEventId())
         .Returns("");

     ProcessTransactionConfig processTransactionConfig = new ProcessTransactionConfig
     {
         DataLakeWriteEnabled = true,
         MaxRetryCount = 3
     };

     var config = new DataLakeConfiguration
     {
         DataLakePaths = "DummyPath"
     };

     _options.Setup(x => x.Value).Returns(config);
     _configuration.Setup(c => c.Value).Returns(processTransactionConfig);

     var sut = new RetailSalesToVendor(_mockDtoHttpService.Object,
                                 _mockAdapterService.Object,
                                 _mockAdapterAuthHttpService.Object,
                                 _mockServiceBusMessageReceiver.Object,
                                 _dateLakeClient.Object,
                                 _options.Object,
                                 _mapper,
                                 _mockServiceBusClient.Object,
                                 _mockLogger.Object,
                                 _configuration.Object);

     //Act
     await sut.RunAsync(testMessage, _messageAction.Object, lockToken);

     //Assert
     _mockServiceBusMessageReceiver.Verify(x => x.ParseServiceBusMessage(testMessage), Times.Once());
     _mockServiceBusMessageReceiver.Verify(x => x.CompleteMessageAsync(It.IsAny<ServiceBusReceivedMessage>(), It.IsAny<ServiceBusMessageActions>(), It.IsAny<CancellationToken>()), Times.Once());
     _mockAdapterService.Verify(x => x.PostAsync(It.Is<List<TransactionDto>>(o => TestEqualityUsingJson(NormalizeDates(o), outputFileContent1)), It.IsAny<string>()), Times.Once());
     if (outputFileContent2 != null)
         _mockAdapterService.Verify(x => x.PostAsync(It.Is<List<TransactionDto>>(o => TestEqualityUsingJson(NormalizeDates(o), outputFileContent2)), It.IsAny<string>()), Times.Once());
     _mockAdapterAuthHttpService.Verify(x => x.GetCurrentWriteTokenAsync(It.IsAny<string>()), Times.AtLeastOnce());
     _mockDtoHttpService.Verify(x => x.GetDtoAsync<SaleDto>(It.IsAny<string>()), Times.AtLeastOnce());
 }

I'm assuming that possibly there is something I need to add to the _mockAdapterService setup to return multiple results and as you can see above, I duplicated the _mockAdapterService.Verify line so I could test the equality of both lines 1 and 2 against the results. I've never had a need to do this kind of testing before, so any suggestions as to how I can handle this would be greatly appreciated.

发布评论

评论列表(0)

  1. 暂无评论