Skip to content

Instantly share code, notes, and snippets.

@TheFo2sh
Created March 20, 2023 00:37
Show Gist options
  • Save TheFo2sh/97d3b22186e956b641c75b6c596173e0 to your computer and use it in GitHub Desktop.
Save TheFo2sh/97d3b22186e956b641c75b6c596173e0 to your computer and use it in GitHub Desktop.
public class PdfGenerationCommandHandlerTests
{
[Fact]
public async Task Handle_ShouldGeneratePdf_WhenValidCommandProvided()
{
// Arrange
var command = new PdfGenerationCommand { HtmlContent = "<html><body><h1>Hello World!</h1></body></html>" };
var requestHandler = new Mock<IRequestHandler<PdfGenerationCommand, PdfGenerationResponse>>();
requestHandler.Setup(x => x.Handle(It.IsAny<PdfGenerationCommand>(), It.IsAny<CancellationToken>(), It.IsAny<IProgress<int>>()))
.ReturnsAsync(new PdfGenerationResponse { PdfContent = new byte[] { 1, 2, 3 }, DocumentId = "abc" });
var handler = new PdfGenerationCommandHandler(requestHandler.Object);
// Act
await handler.Handle(command);
// Assert
// Assert that the request handler is called with the correct command and parameters
requestHandler.Verify(x => x.Handle(command, It.IsAny<CancellationToken>(), It.IsAny<IProgress<int>>()), Times.Once);
// Assert that the response is correctly serialized and written to the console
var expectedResponse = "{\"PdfContent\":[1,2,3],\"DocumentId\":\"abc\"}";
Assert.Equal(expectedResponse, consoleOutput.ToString());
}
}
public class PdfGenerationRequestHandlerTests
{
[Fact]
public async Task Handle_ShouldGeneratePdf_WhenValidHtmlContentProvided()
{
// Arrange
var htmlContent = "<html><body><h1>Hello World!</h1></body></html>";
var generator = new Mock<IPdfGenerator>();
generator.Setup(x => x.GeneratePdfAsync(htmlContent)).ReturnsAsync(new byte[] { 1, 2, 3 });
var command = new PdfGenerationCommand { HtmlContent = htmlContent };
var handler = new PdfGenerationRequestHandler(generator.Object);
// Act
var response = await handler.Handle(command, CancellationToken.None, null);
// Assert
// Assert that the PDF content is generated successfully
// Assert that the PDF content is returned in the response
Assert.Equal(new byte[] { 1, 2, 3 }, response.PdfContent);
// Assert that a document ID is generated and returned in the response
Assert.NotEmpty(response.DocumentId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment