public async Task DownloadAsync_DocumentRepositoryException_IsLogged() { var exception = new DocumentRepositoryException( new InvalidOperationException(), StatusCodes.Status502BadGateway); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .Throws(exception); var logLevel = LogLevel.None; Exception actualException = null; void Callback(LogLevel l, Exception e) { logLevel = l; actualException = e; } var mockLogger = new MockLogger <SolutionsController>(Callback); var controller = new SolutionsController(mockStorage.Object, mockLogger); await controller.DownloadAsync("ID", "directory"); logLevel.Should().Be(LogLevel.Error); actualException.Should().Be(exception); }
public static async Task DownloadAsync_DocumentRepositoryException_IsLogged() { var expectedException = new DocumentRepositoryException( new InvalidOperationException(), StatusCodes.Status502BadGateway); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .Throws(expectedException); var mockLogger = new Mock <ILogger <SolutionsController> >(); var controller = new SolutionsController(mockStorage.Object, mockLogger.Object); await controller.DownloadAsync("ID", "directory"); Expression <Action <ILogger <SolutionsController> > > expression = l => l.Log( It.Is <LogLevel>(l => l == LogLevel.Error), It.IsAny <EventId>(), It.Is <It.IsAnyType>((v, t) => true), It.Is <Exception>(e => e == expectedException), It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true)); mockLogger.Verify(expression); }
public void DownloadAsync_Exception_DoesNotSwallow() { var exception = new InvalidOperationException(); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .Throws(exception); var controller = new SolutionsController(mockStorage.Object, Mock.Of <ILogger <SolutionsController> >()); Assert.ThrowsAsync <InvalidOperationException>(() => controller.DownloadAsync("ID", "directory")); }
public async Task DownloadAsync_DocumentRepositoryException_ReturnsStatusCodeResult() { var exception = new DocumentRepositoryException( new InvalidOperationException(), StatusCodes.Status502BadGateway); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .Throws(exception); var controller = new SolutionsController(mockStorage.Object, Mock.Of <ILogger <SolutionsController> >()); var result = await controller.DownloadAsync("ID", "directory") as StatusCodeResult; Assert.NotNull(result); result.StatusCode.Should().Be(exception.HttpStatusCode); }
public async Task DownloadAsync_ReturnsFileStreamResult() { const string expectedContentType = "test/content-type"; using var expectedStream = new MemoryStream(Encoding.UTF8.GetBytes("Hello world!")); var downloadInfo = new Mock <IDocument>(); downloadInfo.Setup(d => d.Content).Returns(expectedStream); downloadInfo.Setup(d => d.ContentType).Returns(expectedContentType); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(downloadInfo.Object); var controller = new SolutionsController(mockStorage.Object, Mock.Of <ILogger <SolutionsController> >()); var result = await controller.DownloadAsync("ID", "directory") as FileStreamResult; Assert.NotNull(result); result.FileStream.IsSameOrEqualTo(expectedStream); result.ContentType.Should().Be(expectedContentType); }