Tuesday, November 29, 2016

Mocking Unit Tests: HttpClient and 2+ parameters

If you have an HTTP Client unit test, you can try the option of making a Wrapper interface in your solution like this here: http://stackoverflow.com/questions/10693955/stubbing-or-mocking-asp-net-web-api-httpclient. As for two parameters in the unit test, you can do the following..

Guid id = Guid.NewGuid();

string baseUri = ConfigurationManager.AppSettings["BaseUrl"] + @"/"
 + ConfigurationManager.AppSettings["ApiVersion"] + @"/";

Dto responseDto = new Dto();

httpClient.Setup(x => x.PostAsync(new Uri(baseUri + "AdditionalUrlHere/Stuff"), It.IsAny()))
                .Callback((uri, dto) => responseDto = dto)
                .ReturnsAsync(id);