Posts

Showing posts from August, 2021

Mock ApiException on Refit

I use Refit to call web api in my code I also like use StatusCode 422 when the web api have to report that there is a logic error in the application and not an exception and use test, to verify the code. So I want write here about this problem, how, with mock and Refit we can test that the application report correctly the 422 status code and a string error(or anything else) WebApi My web api have a class with a response and an error property. public class Response { public string Error { get; set; } public void SetError(string error) { Error = error; } } And the interface of refit public interface IClient { [Post("/Controller/MyMethod")] Task MyMethod([Body]Parameters parameters); } The working code could be something like this public string ApiCaller(IClient client) { try { await client.MyMethod ( new Parameters(...); ); } catch(ApiException ex) when (ex.StatusCode == HttpStatus