public void ArticleClient_UpdateAsync_test() { #region Setup var handler = new Mock <HttpMessageHandler>(); handler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns((HttpRequestMessage request, CancellationToken cancellationToken) => { HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError); if (request.RequestUri.ToString().EndsWith("article")) { Article article = new JsonFormatter().Deserialize <Article>(request.Content.ReadAsStreamAsync().Result); if (article == null) { httpResponseMessage = new HttpResponseMessage(HttpStatusCode.NotFound); httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject("")); } else { httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK); httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(new Article() { Slug = "Slug" })); } } return(Task.FromResult(httpResponseMessage)); }); Mock <HttpClientFactory> httpClientFactory = new Mock <HttpClientFactory>(new Mock <IDependencyResolver>().Object); httpClientFactory.Setup(p => p.Create()).Returns(() => { return(new HttpClient(handler.Object)); }); Mock <IBaasicClientFactory> baasicClientFactory = new Mock <IBaasicClientFactory>(); baasicClientFactory.Setup(f => f.Create(It.IsAny <IClientConfiguration>())).Returns((IClientConfiguration config) => new BaasicClient(config, httpClientFactory.Object, new JsonFormatter())); Mock <IClientConfiguration> clientConfiguration = new Mock <IClientConfiguration>(); clientConfiguration.Setup(p => p.DefaultMediaType).Returns(ClientConfiguration.HalJsonMediaType); clientConfiguration.Setup(p => p.DefaultTimeout).Returns(TimeSpan.FromSeconds(1)); clientConfiguration.Setup(p => p.ApplicationIdentifier).Returns("Test"); clientConfiguration.Setup(p => p.SecureBaseAddress).Returns("https://api.baasic.com/v1"); clientConfiguration.Setup(p => p.BaseAddress).Returns("http://api.baasic.com/v1"); ArticleClient target = new ArticleClient(clientConfiguration.Object, baasicClientFactory.Object); target.Should().NotBeNull(); #endregion Setup target.UpdateAsync(null).Result.Should().BeNull(); var expected = target.UpdateAsync(new Article() { Id = Guid.NewGuid(), Slug = "Slug" }).Result; expected.Should().NotBeNull(); expected.Slug.Should().Be("Slug"); }
public async Task UpdateAsync() { var apiClient = Substitute.For <IApiConnection>(); var client = new ArticleClient(apiClient); await client.UpdateAsync(1, new UpdateArticle()); await apiClient.Received().ExecutePutAsync <object, Article>("articles/1", Arg.Any <object>()); }
public async Task UpdateAsync_RequestNull_Throw() { var apiClient = Substitute.For <IApiConnection>(); var client = new ArticleClient(apiClient); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await client.UpdateAsync(1, null)); await apiClient.DidNotReceive().ExecutePostAsync <object, Article>("articles/1", Arg.Any <object>()); }
public async Task UpdateAsync_ArticleIdZero_Throw() { var apiClient = Substitute.For <IApiConnection>(); var client = new ArticleClient(apiClient); await Assert.ThrowsExceptionAsync <ArgumentOutOfRangeException>(async() => await client.UpdateAsync(0, new UpdateArticle())); await apiClient.DidNotReceive().ExecutePostAsync <object, Article>("articles/0", Arg.Any <object>()); }