public void GetImage_RestClientFailed_ExceptionThrown() { // Arrange var expected = new byte[0]; var uriAddress = "http://localhost:63219/"; Mock <IRestClient> restClient = new Mock <IRestClient>(MockBehavior.Strict); restClient.Setup(x => x.Execute(It.IsAny <IRestRequest>())).Throws(new Exception()); restClient.SetupSet(x => x.BaseUrl = new Uri(uriAddress)); var imageService = new ExternalImageService { ExternalImageServiceUrl = uriAddress, ExternalImageServiceResource = "image", ExternalImageServiceQueryParameter = "imageName" }; Mock <IOptions <IFictionConfiguration> > configuration = new Mock <IOptions <IFictionConfiguration> >(MockBehavior.Strict); configuration.Setup(x => x.Value.ImageName).Returns("Image_name.png"); configuration.SetupGet(x => x.Value.ExternalImageService).Returns(imageService); var sut = new ExternalImageServiceClient(configuration.Object, restClient.Object); // Act & Assert Assert.Throws <Exception>(() => sut.GetImage()); }
public void GetImage_Success_AllRestClientInvocationsCalled() { // Arrange var expected = new byte[0]; var uriAddress = "http://localhost:63219/"; Mock <IRestClient> restClient = new Mock <IRestClient>(MockBehavior.Strict); restClient.Setup(x => x.Execute(It.IsAny <IRestRequest>())).Returns(new RestResponse { RawBytes = expected }); restClient.SetupSet(x => x.BaseUrl = new Uri(uriAddress)); var imageService = new ExternalImageService { ExternalImageServiceUrl = uriAddress, ExternalImageServiceResource = "image", ExternalImageServiceQueryParameter = "imageName" }; Mock <IOptions <IFictionConfiguration> > configuration = new Mock <IOptions <IFictionConfiguration> >(MockBehavior.Strict); configuration.Setup(x => x.Value.ImageName).Returns("Image_name.png"); configuration.SetupGet(x => x.Value.ExternalImageService).Returns(imageService); var sut = new ExternalImageServiceClient(configuration.Object, restClient.Object); // Act byte[] result = sut.GetImage(); // Assert Assert.Equal(expected, result); restClient.Verify(x => x.Execute(It.IsAny <IRestRequest>()), Times.Once); restClient.VerifySet(x => x.BaseUrl = new Uri(uriAddress), Times.Once); }
public void GetImage_Success_AllRestClientInvocationsCalled() { //Arrange var uri = new Uri("http://localhost:56227"); var expected = new byte[0]; Mock <IRestClient> restClient = new Mock <IRestClient>(MockBehavior.Strict); restClient.Setup(x => x.Execute(It.IsAny <IRestRequest>())).Returns(new RestResponse { RawBytes = expected }); restClient.SetupSet(x => x.BaseUrl = uri); Mock <IOptions <FictionConfiguration> > configurationOptions = new Mock <IOptions <FictionConfiguration> >(MockBehavior.Strict); configurationOptions.SetupGet(x => x.Value).Returns(new FictionConfiguration { ImageClientUrl = "http://localhost:56227" }); ExternalImageServiceClient sut = new ExternalImageServiceClient(restClient.Object, configurationOptions.Object); //Act var result = sut.GetImage(); //Assert Assert.Equal(expected, result); restClient.Verify(x => x.Execute(It.IsAny <IRestRequest>()), Times.Once); restClient.VerifySet(x => x.BaseUrl = uri, Times.Once); }
public void GetImage_RestClientFailed_ExceptionThrown() { //Arrange var uri = new Uri("http://localhost:56227"); var expected = new byte[0]; Mock <IRestClient> restClient = new Mock <IRestClient>(MockBehavior.Strict); restClient.Setup(x => x.Execute(It.IsAny <IRestRequest>())).Throws(new Exception()); restClient.SetupSet(x => x.BaseUrl = uri); Mock <IOptions <FictionConfiguration> > configurationOptions = new Mock <IOptions <FictionConfiguration> >(MockBehavior.Strict); configurationOptions.SetupGet(x => x.Value).Returns(new FictionConfiguration { ImageClientUrl = "http://localhost:56227" }); ExternalImageServiceClient sut = new ExternalImageServiceClient(restClient.Object, configurationOptions.Object); //Act & Asssert Assert.Throws <Exception>(() => sut.GetImage()); }