0

我想测试一个使用 IHttpClientFactory 接口的存储库。我想测试的存储库使用 httpclient 对 Web 服务进行外部 api 调用。Web 服务返回 json 数据,然后返回反序列化的 json 数据。这是流程遵循我的存储库类中的所有方法。我已经阅读了很多关于这件事的文章,但没有一篇有帮助,也许是因为我是初学者。任何帮助都将受到高度赞赏。这是存储库代码

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;

    }}

``

4

0 回答 0