Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HttpCall Mockable #304

Closed
rcollette opened this issue Apr 3, 2018 · 4 comments
Closed

Make HttpCall Mockable #304

rcollette opened this issue Apr 3, 2018 · 4 comments

Comments

@rcollette
Copy link

rcollette commented Apr 3, 2018

Since there is no public constructor and HttpCall has no virtual implementation (no interface), it is not readily mocked, at least not without having to buy a framework to do so at an IL level.

 try
 {
    result = await _service.GetSomethingAsync(id);
 }
 catch (FlurlHttpException ex) when (ex.Call?.HttpStatus == HttpStatusCode.NotFound)
 {
    throw new NotFoundException($"Could not find with id {id}");
}

I can guess that there will be a response like "catch the exception where the call to Flurl is made and and throw your own exception". However, we then get into a non-DRY scenario or limiting access to the otherwise useful information in the FlurlHttpCall.

What would be the argument against exposing FlurthHttpCall's constructor and enough of the setup/setter functionality such that we can mock for unit tests accordingly?

@tmenier
Copy link
Owner

tmenier commented Apr 3, 2018

It has a public constructor. 3 actually. :)

@rcollette rcollette changed the title Make FlurlHttpException Mockable Make FlurlHttpCall Mockable Apr 4, 2018
@rcollette
Copy link
Author

Sorry, my mistake. I should have said HttpCall. I have updated the issue accordingly.

@rcollette rcollette changed the title Make FlurlHttpCall Mockable Make HttpCall Mockable Apr 4, 2018
@tmenier
Copy link
Owner

tmenier commented Apr 4, 2018

Ah, right you are. I agree, having a public constructor would make it more easily testable. I'm not sure off hand whether making one or both of the existing ones public would be most appropriate or whether creating a new one would be better (feel free to give me your preference), but I'll look into it for the next maintenance release.

@tmenier tmenier added this to the Fllurl.Http 2.3.1 milestone Apr 4, 2018
@tmenier tmenier modified the milestone: Fllurl.Http 2.3.1 Apr 16, 2018
@tmenier tmenier added ready and removed in progress labels Apr 17, 2018
@tmenier
Copy link
Owner

tmenier commented Apr 17, 2018

This is implemented and released in Flurl.Http 2.3.1.

@tmenier tmenier closed this as completed Apr 17, 2018
@tmenier tmenier removed the ready label Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants