0

i want to test a repository that uses the IHttpClientFactory interface.The repository i want to test makes an external api call to a web service using the httpclient.The web service returns json data i then return the Deserialized json data.This is the flow am following in all the methods in my repository class. i have read alot of articles regarding this matter but none were helpful,maybe its because am a beginner.any help will be highly appreciated. here is the repository code ``

public class Movies : IMovies
{
    private readonly IHttpClientFactory _httpClientFactory;

    public Movies(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }

    public async Task<MovieCollection> GetPopularMovies(int PageNumber = 1)
    {

        // Get an instance of HttpClient from the factpry that we registered
        // in Startup.cs
         var client = _httpClientFactory.CreateClient("Movie Api");

        // Call the API & wait for response. 
        // If the API call fails, call it again according to the re-try policy
        // specified in Startup.cs
        var result =
            await client.GetAsync($"movie/popular?api_key=18783d19189b3c4c0abc3c8e3080d3da&language=en-US&page={PageNumber}");
        if (result.IsSuccessStatusCode)
        {
            // Read all of the response and deserialise it into an instace of

            var content = await result.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<MovieCollection>(content);
        }

        return null;

    }}

``

David Tembo
  • 79
  • 3
  • 10
  • Probably with Mocks. Check NSubstitute in google – asaf92 May 12 '20 at 11:59
  • i tried the suggested solution but did not work ....some method in the suggested solution is not working and am stuck..don't know what to do. The method is ``request.CreateResponse`` .Here is the link to the suggested solution to my problem [How to mock the new HttpClientFactory in .NET Core 2.1 using Moq](https://stackoverflow.com/questions/44715602/mock-httpclient-using-moq/44716910#44716910) – David Tembo May 13 '20 at 08:56

0 Answers0