at FiscalPrinterApi.Test.Services.FiscalPrinterServiceTests.PrintFiscalDocument_ShouldReturnSuccess() in C:\Users\adala\OneDrive\Dokumenty\Posnet\Posnet\FiscalPrinterApi.Test\Services\FiscalPrinterServiceTests.cs:line 52
AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
FiscalPrinterServiceTests.PrintFiscalDocument_ShouldReturnSuccess()
The test is failing because the method SendCommandToFiscalPrinter in your FiscalPrinterService is not being mocked, so it's using the actual implementation, which likely fails in the test environment.
There are several issues in your test that need to be addressed:
Incorrect Use of It.IsAny<T>() in Method Calls:
You should not use It.IsAny<T>() as actual parameters when calling the method under test. It.IsAny<T>() is meant for setting up mocks, not for actual method arguments. You need to provide actual values when calling methods.
Incorrect Initialization of Collections:
In your request object initialization, you're using square brackets [ ] which is not valid in C#. You should use new List<T> { ... } or an array initializer new[] { ... }.
Mocking Dependencies:
The SendCommandToFiscalPrinter method needs to be mocked or injected so that it doesn't execute the real implementation during testing.
Here's how you can fix the test:
1. Correct the Request Initialization:
Replace the square brackets with proper collection initializations:
csharp
var request = new ParagonRequest
{
Lines = new List<LineItem> { new LineItem { Na = "Test", Il = 1, Vt = 1, Pr = 100 } },
Payments = new List<Payment> { new Payment { Ty = 1, Wa = 100 } },
Summary = new Summary { To = 100, Fp = 100 }
};
2. Provide Actual Parameters When Calling the Method:
Replace It.IsAny<string>() and It.IsAny<int>() with actual values:
csharp
var response = await fiscalPrinterService.PrintFiscalDocument(request, "127.0.0.1", 12345, false);
3. Mock the SendCommandToFiscalPrinter Method:
Modify the FiscalPrinterService class to make SendCommandToFiscalPrinter virtual:
csharp
public class FiscalPrinterService
{
// Other code...
public virtual async Task<string> SendCommandToFiscalPrinter(string[] commands, string ip, int port)
{
// Original implementation
}
// Other code...
}
Then, in your test, create a derived class that overrides SendCommandToFiscalPrinter:
csharp
public class TestFiscalPrinterService : FiscalPrinterService
{
public override Task<string> SendCommandToFiscalPrinter(string[] commands, string ip, int port)
{
// Return "OK" for testing purposes
return Task.FromResult("OK");
}
}
Now, in your test, use TestFiscalPrinterService instead of FiscalPrinterService:
csharp
[Test]
public async Task PrintFiscalDocument_ShouldReturnSuccess()
{
// Arrange
var fiscalPrinterService = new TestFiscalPrinterService();
var request = new ParagonRequest
{
Lines = new List<LineItem> { new LineItem { Na = "Test", Il = 1, Vt = 1, Pr = 100 } },
Payments = new List<Payment> { new Payment { Ty = 1, Wa = 100 } },
Summary = new Summary { To = 100, Fp = 100 }
};
// Act
var response = await fiscalPrinterService.PrintFiscalDocument(request, "127.0.0.1", 12345, false);
// Assert
Assert.That(response.Ok, Is.True);
Assert.That(response.Code, Is.EqualTo(0));
}
4. (Optional) Use Dependency Injection for Better Testability:
Alternatively, modify FiscalPrinterService to accept dependencies via its constructor:
csharp
public class FiscalPrinterService
{
private readonly IFiscalPrinterService _service;
public FiscalPrinterService(IFiscalPrinterService service)
{
_service = service;
}
public async Task<ParagonResponse> PrintFiscalDocument(ParagonRequest request, string ip, int port, bool bulkMode)
{
// Use _service.SendCommandToFiscalPrinter instead of direct calls
}
}
Then, in your test, you can inject the mocked service:
csharp
[Test]
public async Task PrintFiscalDocument_ShouldReturnSuccess()
{
// Arrange
var mockService = new Mock<IFiscalPrinterService>();
mockService.Setup(service =>
service.SendCommandToFiscalPrinter(It.IsAny<string[]>(), It.IsAny<string>(), It.IsAny<int>()))
.ReturnsAsync("OK");
var fiscalPrinterService = new FiscalPrinterService(mockService.Object);
var request = new ParagonRequest
{
Lines = new List<LineItem> { new LineItem { Na = "Test", Il = 1, Vt = 1, Pr = 100 } },
Payments = new List<Payment> { new Payment { Ty = 1, Wa = 100 } },
Summary = new Summary { To = 100, Fp = 100 }
};
// Act
var response = await fiscalPrinterService.PrintFiscalDocument(request, "127.0.0.1", 12345, false);
// Assert
Assert.That(response.Ok, Is.True);
Assert.That(response.Code, Is.EqualTo(0));
}
Summary:
Replace square brackets with proper initialization syntax.
Provide actual IP and port values instead of It.IsAny<T>() when calling the method.
Mock or override the SendCommandToFiscalPrinter method so that it doesn't execute the real implementation during testing.
By making these changes, the test should execute properly and pass as expected.