Poniższy test napisany w nUnit (C#) zwraca poniższy błąd. Popraw implementację, aby test…
- User
Poniższy test napisany w nUnit (C#) zwraca poniższy błąd. Popraw implementację, aby test wykonywał się prawidłowo.
/// TEST
[Test] public async Task PrintSaleConfirmationAsync_ValidInput_SuccessfulPrint() { // Arrange var response = new FirstDataStartResponse { AcceptorTransactionIdentifier = "123456", TransactionNumber = 1, TID = "TID001", MID = "MID001", CardAcceptorName = "Test Merchant", MaskedCardNumber = "************1234", CardName = "VISA", TransactionCurrencyCode = "PLN", TransactionAmount = 100.00m, MaskedExpiryDateOfCard = "12/24", UnmaskedExpiryDateOfCard = "12/24" }; string ip = "192.168.1.1"; int port = 9100;
var mockFiscalPrinterService = new Mock<FiscalPrinterService>(); mockFiscalPrinterService.Setup(x => x.SendCommandToFiscalPrinter(It.IsAny<string[]>(), ip, port)) .ReturnsAsync(string.Empty); // Simulate successful command execution // Act await mockFiscalPrinterService.Object.PrintSaleConfirmationAsync(response, ip, port); // Assert mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formstart"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl1"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl2"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl4"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl6"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl8"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl9"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl31"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl35"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl70"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formend"), ip, port), Times.Once); }/// KOD
public async Task<string> SendCommandToFiscalPrinter(string[] commands, string ip, int port) { Logger.Debug("Sending command to fiscal printer."); if (string.IsNullOrEmpty(ip)) { throw new InvalidOperationException("The fiscal printer host is not configured. Please set the host before sending commands."); } using var client = new TcpClient(); await client.ConnectAsync(ip, port); using NetworkStream stream = client.GetStream(); var fullCommand = FullCommandCrced(commands); return await SendCommandToStream(fullCommand, stream); } - Assistant
Aby test wykonywał się prawidłowo, musisz umożliwić Moq zastępowanie metody
SendCommandToFiscalPrinter. Domyślnie Moq może zastępować tylko metody wirtualne lub interfejsy. Twoja metodaSendCommandToFiscalPrinternie jest wirtualna, więc Moq nie może jej zastąpić, co powoduje błąd.Aby to naprawić, zmień definicję metody
SendCommandToFiscalPrinter, dodając słowo kluczowevirtual:public class FiscalPrinterService { public virtual async Task<string> SendCommandToFiscalPrinter(string[] commands, string ip, int port) { Logger.Debug("Sending command to fiscal printer."); if (string.IsNullOrEmpty(ip)) { throw new InvalidOperationException("The fiscal printer host is not configured. Please set the host before sending commands."); } using var client = new TcpClient(); await client.ConnectAsync(ip, port); using NetworkStream stream = client.GetStream(); var fullCommand = FullCommandCrced(commands); return await SendCommandToStream(fullCommand, stream); } // Upewnij się, że metoda PrintSaleConfirmationAsync jest również wirtualna, jeśli potrzebujesz jej zastępowania public virtual async Task PrintSaleConfirmationAsync(FirstDataStartResponse response, string ip, int port) { // Implementacja metody, która wywołuje SendCommandToFiscalPrinter } // Reszta klasy... }