简体   繁体   English

如何对 Refit ApiResponse 进行单元测试<T> ?

[英]How to unit test the Refit ApiResponse<T>?

I'm using refit to call APIs and the response is wrapped in ApiResonse<T> .我正在使用 refit 来调用 API,并且响应包含在ApiResonse<T>中。 I can mock the refit call that returns a mocked ApiResponse<T> and assert.我可以模拟返回模拟的ApiResponse<T>和断言的改装调用。 Everything works as it should except I'm not sure how I can test the api call that returns an exception.一切正常,除了我不确定如何测试返回异常的 api 调用。

[Get("/customer/{id}")]
Task<ApiResponse<Customer>> GetCustomer(int id);

It seems if ApiResponse<T> is used Refit wraps all the exceptions that happens inside the request into ApiException and attaches to ApiResponse<T> .如果使用ApiResponse<T>似乎 Refit 会将请求中发生的所有异常包装到ApiException并附加到ApiResponse<T> If Task<T> is used instead, catching the ApiException is the responsibility of the caller.如果改为使用Task<T> ,则捕获ApiException是调用者的责任。

I created the ApiException with ApiException.Create() but not sure where to put that in the ApiResponse<T> .我使用ApiException.Create()创建了ApiException ,但不确定将其放在ApiResponse<T>中的哪个位置。 The last parameter to the Create() method is ApiException but the exception I create and set doesn't show up in the ApiResponse at all and therefore can't test. Create()方法的最后一个参数是 ApiException,但我创建和设置的异常根本没有出现在 ApiResponse 中,因此无法测试。

var apiResponse = new ApiResponse<Customer>(
    new HttpResponseMessage(BadRequest),
    null,
    null,
    await ApiException.Create(,,,Exception("Something bad happened inside the api")
 );

I can't get the Something bad happened... message in the unit test but just the 'Bad Request' message.我无法在单元测试中收到“发生了错误的Something bad happened...消息,而只有“错误请求”消息。 Am I unit testing the ApiResponse in refit right?我是否对改装中的 ApiResponse 进行了单元测试,对吗? Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks谢谢

I don't know Refit, but according to their docs I'd expect the following two options:我不知道改装,但根据他们的文档,我希望有以下两个选项:

Return ApiResponse<Customer>返回ApiResponse<Customer>

Let's take a look at your code (which is not correct C#, by the way - please provide correctly working code):让我们看一下您的代码(顺便说一下,这不是正确的 C# - 请提供正确工作的代码):

var apiResponse = new ApiResponse<Customer>(
    new HttpResponseMessage(BadRequest),
    null,
    null,
    await ApiException.Create(,,,Exception("Something bad happened inside the api")
 );

When executing this code, Refit is instructed to return exactly what you ask for: return HTTP 500 with the following inner exception .执行此代码时,会指示 Refit 准确返回您所要求的内容:返回HTTP 500并带有以下内部异常

So I'd expect you'll find Something bad happened inside the api inside apiResponse.Error , but no exception will be thrown.所以我希望你会发现在apiResponse.Error的 api 中发生了一些不好的事情,但不会抛出异常。

Throw ApiException抛出ApiException

Since ApiException derives from System.Exception , you can replace this由于ApiException派生自System.Exception ,因此您可以替换它

var apiResponse = new ApiResponse<Customer>(
    new HttpResponseMessage(BadRequest),
    null,
    null,
    await ApiException.Create(,,,Exception("Something bad happened inside the api")
 );

by经过

throw await ApiException.Create("Something bad happened inside the api", message, method, response, settings);

Now you can catch your exception inside your unit test.现在您可以在单元测试中捕获您的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM