-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
An simple way to mock an httpClient.GetAsync(..) method for unit tests? #14535
Comments
In other works, |
But if feels like sooo much work is required to setup the message handler when (it feels like) this could be handled with a nice interface? Am I totally misunderstanding the solution? |
@PureKrome - thanks for bringing this up for discussion. Can you please elaborate on what you mean by "so much work is required to setup the message handler"? One way to unit test HttpClient without hitting the network is -
Your HttpClient object will then use your Handler instead of the inbuilt HttpClientHandler. Thanks, |
Hi @SidharthNabar - Thank you for reading my issue, I really appreciate it also.
You just answered my question :) That's a large dance to wiggle too, just to ask my real code to not hit the network. I even made a the HttpClient.Helpers repo and nuget package .. just to make testing easier! Scenario's like the Happy path or an exception thrown by the network endpoint ... That's the problem -> can we not do all of this and ... just a mock a method instead? I'll try and explain via code.. Goal: Download something from the interwebs.
Lets look at two examples: Given an interface (if it existed):
My code can now look like this...
and the test class
Yay! done. Ok, now with the current way...
But the pain is now that I have to make a class. You've now hurt me.
And this class starts out pretty simple. Until I when I have multiple eg.
this can be made so much easier with this..
clean clean clean :) Then - there is the next bit : Discoverability When I first started using ... but then I hit testing .... and now I'm suppose to learn about It's all making this more complex than it should be, IMO. |
I too would love to see an easy and simple way to test various things that use HttpClient. 👍 |
Thanks for the detailed response and code snippets - that really helps. First, I notice that you are creating new instances of HttpClient for sending each request - that is not the intended design pattern for HttpClient. Creating one HttpClient instance and reusing it for all your requests helps optimize connection pooling and memory management. Please consider reusing a single instance of HttpClient. Once you do this, you can insert the fake handler into just one instance of HttpClient and you're done. Second - you are right. The API design of HttpClient does not lend itself to a pure interface-based mechanism for testing. We are considering adding a static method/factory pattern in the future that will allow you to alter the behavior of all HttpClient instances created from that factory or after that method. We haven't yet settled on a design for this yet, though. But the key issue will remain - you will need to define a fake Handler and insert it below the HttpClient object. @ericstj - thoughts on this? Thanks, |
@SidharthNabar why are you/team so hesitant to offering an interface for this class? I was hoping that the whole point of a developer having to learn about handlers and then having to create fake handler classes is enough of a pain point to justify (or at the very least, highlight) the need for an interface? |
Yeah, I don't see why an Interface would be a bad thing. It would make testing HttpClient so much easier. |
One of the main reasons we avoid interfaces in the framework is that they don't version well. Folks can always create their own if they have a need and that won't get broken when the next version of the framework needs to add a new member. @KrzysztofCwalina or @terrajobst might be able to share more of the history/detail of this design guideline. This particular issue is a near religious debate: I'm too pragmatic to take part in that. HttpClient has lots of options for unit testability. It's not sealed and most of its members are virtual. It has a base class that can be used to simplify the abstraction as well as a carefully designed extension point in HttpMessageHandler as @sharwell points out. IMO it's a quite well designed API, thanks to @HenrikFrystykNielsen. |
👍 an interface |
👋 @ericstj Thanks heaps for jumping in 👍 🍰
Yep - great point.
yeah ... point well taken on that.
Oh? I'm struggling on this 😊 hence the reason for this issue 😊
Members are virtual? Oh drats, I totally missed that! If they are virtual, then mocking frameworks can mock those members out 👍 and we don't need an interface! Lets have a looksies at HttpClient.cs ...
hmm. those aren't virtual ... lets search the file ... er .. no I guess I'm just not understanding something really basic, here ¯(°_°)/¯ ? EDIT: maybe those methods can be virtual? I can PR! |
SendAsync is virtual and almost every other API layers on top of that. 'Most' was incorrect, my memory serves me wrong there. My impression was that most were effectively virtual since they build on top of a virtual member. Usually we don't make things virtual if they are cascading overloads. There is a more specific overload of SendAsync that is not virtual, that one could be fixed. |
Ah! Gotcha 😊 So all those methods end up calling How would we mock
|
I can totally understand this way of thinking with the full .NET Framework. However, .NET Core is split into many small packages, each versioned independently. Use semantic versioning, bump the major version when there's a breaking change, and you're done. The only people affected will be the ones explicitly updating their packages to the new major version - knowing there are documented breaking changes. I'm not advocating that you should break every interface every day: breaking changes should ideally be batched together into a new major version. I find it sad to cripple APIs for future compatibility reasons. Wasn't one of the goal of .NET Core to iterate faster since you don't have to worry anymore about a subtle .NET update breaking thousands of already installed applications? My two cents. |
@MrJul 👍 |
Versioning is still very much an issue. Adding a member to an interface is a breaking change. For core libraries like this that are inbox in desktop we want to bring back features to desktop in future versions. If we fork it means folks can't write portable code that will run in both places. For more information on breaking changes have a look at: https://github.com/dotnet/corefx/wiki/Breaking-Changes. I think this is a good discussion, but as I mentioned before I don't have a lot of scratch in the argument around interfaces vs abstract classes. Perhaps this is a topic to bring to the next design meeting? I'm not super familiar with the test library you're using, but what I'd do is provide a hook to allow tests to set the instance of the client and then create a mock object that behaved however I want. The mock object could be mutable to permit some reuse. If you have a specific compatible change you'd like to suggest to HttpClient that improves unit testability please propose it and @SidharthNabar and his team can consider it. |
If the API is not mockable, we should fix it. But it has nothing to do with interfaces vs classes. Interface is no different than pure abstract class from mockability perspective, for all practical purposes. |
@KrzysztofCwalina you, sir, hit the nail on the head! perfect summary! |
I guess I'm going to take the less popular side of this argument as I personally do not think an interface is necessary. As has already been pointed out, So let's address the "I need to create my own Dependency wise, your service class should allow an In closing, I find this API perfectly testable in its current form and see no need for an interface. Yes, it requires a different approach, but the aforementioned libraries already exist to help us with this differnt style of testing in perfectly logical, intuitive ways. If we want more functionality/simplicity let's contribute to those libraries to make them better. |
Personally, an Interface or virtual methods doesn't matter that much to me. But c'mon, |
@luisrudge Well can you give a scenario that can't be tested using the message handler style of testing that something like MockHttp enables? Maybe that would help make the case. I haven't come across anything I couldn't codify yet, but maybe I'm missing some more esoteric scenarios that can't be covered. Even then, might just be a missing feature of that library that someone could contribute. For now I maintain the opinion that it's "perfectly testable" as a dependency, just not in a way .NET devs are used to. |
It's testable, I agree. but it's still a pain. If you have to use an external lib that helps you do that, it's not perfectly testable. But I guess that's too subjective to have a discussion over it. |
One could argue that aspnet mvc 5 is perfectly testable, you just have to write 50LOC to mock every single thing a controller needs. IMHO, that's the same case with HttpClient. It's testable? Yes. It's easy to test? No. |
@luisrudge Yes, I agree, it's subjective and I completely understand the desire for interface/virtuals. I'm just trying to make sure anyone who comes along and reads this thread will at least get some education on the fact that this API can be leveraged in a code base in a very testable way without introducing all of your own abstractions around
Well, we're all using one library or another for mocking/faking already* and, it's true, we can't just use that one for testing this style of API, but I don't think that means its any less testable than an interface based approach just because I have to bring in another library. * At least I hope not! |
@drub0y from my OP I've stated that the library is testable - but it's just a real pain compared (to what i passionately believe) to what it could be. IMO @luisrudge spelt it out perfectly :
This repo is a major part of a large number of developers. So the default (and understandable) tactic is to be very cautious with this repo. Sure - I totally get that. I'd like to believe that this repo is still in a position to be tweaked (with respect to the API) before it goes RTW. Future changes suddenly become really hard to do, include the perception to change. So with the current api - can it be tested? Yep. Does it pass the Dark Matter Developer test? I personally don't think so. The litmus test IMO is this: can a regular joe pickup one of the common/popular test frameworks + mocking frameworks and mock any of the main methods in this API? Right now - nope. So then the developer needs to stop what they're doing and start learning about the implimentation of this library.
This is my point - why are you making us having to spend cycles figuring this out when the library's purpose is to abstract all that magic .. only to say "Ok .. so we've done some magic but now that you want to mock our magic ... I'm sorry, you're now going to have to pull up your sleeves, lift up the roof of the library and start digging inside, etc". It feels so ... convoluted. We're in a position to make life easy for so many people and to keep coming back to the defensive position of : "It can be done - but .. read the FAQ/Code". Here's another angle to approach this issue: Give 2 example to random Non-MS devs ... joe citizens developers that know how to use xUnit and Moq/FakeItEasy/InsertTheHipMockingFrameworkThisYear.
It distills down to that, IMO. See which developer can solve that problem and stay happy.
Right now it's not IMO - but there's ways to get around this successfully (again, that's opinionated - I'll concede that)
Exactly - that's an implementation detail. Goal is to be able to use a battle-tested mock framework and off you go.
(I hope I understood that last quote/paragraph..) Not ... quiet. What @luisrudge was saying is: "We have one tool for testing. A second tool for mocking. So far, those a generic/overall tools not tied to anything. But .. you now want me to download a 3rd tool which is specific to mocking a specific API/service in our code because that specific API/service we're using, wasn't designed to be tested nicely?". That's a bit rich :(
Completely agreed! So, can you refactor the sample code above to show us how easy this should/can be? There's many ways to skin a cat ... so what's a simple AND easy way? Show us the code. I'll start. Appologies to @KrzysztofCwalina for using an interface, but this is just to kickstart my litmus test.
|
It sounds like all @PureKrome needs is a documentation update explaining which methods to mock/override in order to customize the behavior of the API during testing. |
@sharwell that's absolutely not the case. When I test stuff, I don't want to run the entire httpclient code: |
@luisrudge Actually my suggestion would be to avoid mocking for this library altogether. Since you already have a clean abstraction layer with a decoupled, replaceable implementation, additional mocking is unnecessary. Mocking for this library has the following additional downsides:
Mocking is a testing strategy targeting intertwined codebases that are difficult to test at a small scale. While the use of mocking correlates with increased development costs, the need for mocking correlates with increased amount of coupling in the code. This means mocking itself is a code smell. If you can provide the same input-output test coverage across and within your API without using mocking, you will benefit in basically every development aspect. |
With respect, if the option is having to write your own Faked Message Handler, which isn't obvious without digging through the code, then that is a very high barrier that is going to be hugely frustrating for a lot of developers. I agree with @luisrudge 's comment earlier. Technically MVC5 was testable, but my god was it a pain in the ass and an immense source of hair pulling. |
It's near impossible to share an instance of HttpClient imn any really application as soon as you need to send different HTTP header on each request (what is crucial when communicating with properly designed RESTful web services). Currently |
@abatishchev But you can specify headers on each HttpRequestMessage. |
@richardszalay I don't say it's completely impossible, I say that HttpClient wasn't designed well for this purpose. None of |
Meeting 99% of the needs. |
Does it mean setting headers is 1% of use cases? I doubt. Either way this won't be an issue if those methods had an overload accepting HttpResponseMessage. |
@abatishchev I don't doubt it, but either way, I'd write extension methods if I found myself in your scenario. |
I toyed with the idea of maybe interfacing HttpMessageHandler and possibly HttpRequestMessage because I didn't like having to write fakes (vs. mocks). But the further down that rabbit hole you go, the more you realize that you'll be trying to fake actual data value objects (e.g. HttpContent) which is a futile exercise. So I think that designing your dependent classes to optionally take HttpMessageHandler as a ctor argument and using a fake for unit tests is the most appropriate route. I'd even argue that wrapping HttpClient is also a waste of time... This will allow you to unit test your dependent class without actually making a call to the internet, which is what you want. And your fake can return pre-determined status codes and content so that you can test that your dependent code is processing them as expected, which is again, what you actually want. |
@dasjestyr Did you try creating an interface for /me curious. |
@PureKrome I sketched out creating an interface for it and that's where I quickly realized that it was pointless. HttpClient really just abstracts a bunch of stuff that doesn't matter in the context of unit testing, and then calls the message handler (which was a point that was made a few times in this thread). I also tried creating a wrapper for it, and that was simply not worth the work required to implement it or propagate the practice (i.e. "yo, everyone do this instead of using HttpClient directly"). It's MUCH easier to simply focus on the handler, as it gives you everything you need and is literally comprised of a single method. That said, I have created my own RestClient, but that solved a different problem which was providing a fluid request builder, but even that client accepts a message handler that can be used for unit testing or for implementing custom handlers that handle things like handler chains that solve cross-cutting concerns (e.g. logging, auth, retry logic, etc.) which is the way to go. That's not specific to my rest client, that's just a great use-case for setting the handler. I actually like the HttpClient interface in the Windows namespace much better for this reason, but I digress. I think it could still be useful to interface the handler, however, but it would have to stop there. Your mocking framework can then be setup to return pre-determined instances of HttpResponseMessage. |
Interesting. I've found (personal bias?) my helper library works great when using a (fake) concrete message handler .. vs.. some interface stuff on that lowish level. I would still prefer not to have to write that library or use it, though :) |
I don't see any problem with creating a small library to build fakes. I might do so when I'm bored with nothing else to do. All my http stuff is already abstracted and tested so I have no real use for it at the moment. I just don't see any value in wrapping the HttpClient for the purpose of unit testing. Faking the handler is all you really need. Extending functionality is a completely separate topic. |
When the most of the codebase is tested using mocking interfaces, it's more convenient and consistent when the rest of the codebase is tested the same way. So I would like to see an interface IHttpClient. Like IFileSystemOperarions from ADL SDK or IDataFactoryManagementClient from ADF management SDK, etc. |
I still think you're missing the point, which is HttpClient doesn't need to be mocked, only the handler. The real problem is the way that people look at HttpClient. It's not some random class that should be newed up whenever you think to call the internet. In fact, it's best practice to reuse the client across your entire application -- it's that big of a deal. Pull the two things apart, and it makes more sense. Plus, your dependent classes shouldn't care about the HttpClient, only the data that gets returned by it -- which comes from the handler. Think of it this way: are you ever going to replace the HttpClient implementation with something else? Possible... but not likely. You never need to change the way the client works, so why bother abstracting it? The message handler is the variable. You'd want to change how the responses get handled, but not what the client does. Even the pipeline in WebAPI is focused on the handler (see: delegating handler). The more I say it, the more I start to think that .Net should make the client static and manage it for you... but I mean... whatever. Remember what interfaces are for. They're not for testing -- it was just a clever way to leverage them. Creating interfaces solely for that purpose is ridiculous. Microsoft gave you what you need to decouple the message handling behavior, and it works perfectly for testing. Actually, HttpMesageHandler is abstract, so I think most mocking frameworks like Moq would still work with it. |
Heh @dasjestyr - I too think you might have missed a major point of my discussion. The fact that we (the developer) needs to learn so much about Message Handlers, etc .. to fake the response is my main point about all of this. Not so much about interfaces. Sure, I (personally) prefer interfaces to virtuals r wrappers with respect to testing (and therefore mocking) ... but those are implementation details. I'm hoping the main gist of this epic thread is to highlight that .. when using HttpClient in an application, it's a PITA to test with it. The status quo of "go learn the plumbing of HttpClient, which will lead you to HttpMessageHandler, etc" is a poor situation. We don't need to do this for many other libraries, etc. So I was hoping something can be done to alleviate this PITA. Yes, the PITA is an opinion - I'll totally admit to that. Some people don't think it's a PITA at all, etc.
Agreed! But until fairly recently, this wasn't well known - which might lead to some lack of documentation or teaching or something.
Yep - agreed.
a what? huh? Oh -- now u're asking me to open up the hood and learn about the plumbing? ... Refer to above again and again.
No. .. etc .. Now, I'm not meaning to troll, etc. So please don't think that I'm trying to be a jerk by always replying and repeating the same stuff over and over again. I've been using my helper library for ages now which is just a glorified way of using a custom message handler. Great! It works and works great. I just think that this could be all exposed a bit nicer ... and that's what I'm hoping this thread really hits home at. EDIT: formatting. |
(I've only just noticed the grammar error in this issue's title and now I can't unsee it) |
I know! Same! |
wut? Have you even looked at HttpMessageHandler? It's an abstract class that is literally a single method that takes an HttpRequestMessage and returns a HttpResponseMessage -- made to intercept behavior separate of all the low level transport junk which is exactly what you'd want in a unit test. To fake it, just implement it. The message that goes in is the one that you sent HttpClient, and the response that comes out is up to you. For example, if I want to know that my code is dealing with a json body correctly, then just have the response return a json body that you expect. If you want to see if it's handling 404 correctly, then have it return a 404. It doesn't get more basic than that. To use the handler, just send it in as a ctor argument for HttpClient. You don't need to pull any wires out or learn the internal workings of anything.
And the main gist of what many people have pointed out is that you're doing it wrong (which is why it's a PITA, and I say this directly but respectfully) and demanding that HttpClient be interfaced for testing purposes is akin to creating features to compensate for bugs instead of addressing the root problem which is, in this case, that you're operating at the wrong level. I think that HttpClient in the windows namespace actually did separate the concept of handler into filter, but it does the exact same thing. I think that handler/filter in that implementation is actually interfaced though which is kinda what I was suggesting earlier. |
Initially no, because of this:
meaning, the exposed methods on Ok - so now lets use it in a test ... and now we need to spend time learning what happens underneath .. which leads us to learning about Again I keep saying this => this extra learning about the plumbing of
Yep - people disagree with me. No probs. Also - people agree me too. Again, no probs.
Ok - I respectfully disagree with you here (which is ok). Again, different opinions. But srsly. This thread has gone on for so long. I've really moved on by now. I said my piece, some people say YES! some said NO! Nothing has changed and I've just ... accepted the status quo and rolled with everything. I'm sorry you just don't accept my train of thought. I'm not a bad person and try not sound rude or mean. This was just me expressing how I felt while developing and how I believe others also felt. That's all. |
I'm not at all offended. I originally hopped on this thread with the same opinion that the client should be mockable, but then some really good points were made about what HttpClient is so I sat and really thought about it, and then tried to challenge it on my own and eventually I came to the same conclusion which is that HttpClient is the wrong thing to mock and trying to do so is futile exercise that causes far more work than it's worth. Accepting that made life much easier. So I too have moved on. I'm just hoping that others will eventually give themselves a break and take it for what it is. |
As an aside:
I'd argue that in terms of SOLID, this interface is inappropriate anyway. IMO client, request, response are 3 different responsibilities. I can appreciate the convenience methods on the client, but it promotes tight coupling by combining requests and responses with the client, but that's personal preference. I have some extensions to HttpResponseMessage that accomplish the same thing but keep the responsibility of reading the response with the response message. In my experience, with large projects, "Simple" is never "Simple" and almost always ends in a BBOM. This however, is a completely different discussion :) |
Now that we have a new repo specifically for design discussions, please continue the discussion in https://github.com/dotnet/designs/issues/9 or open a new issue in https://github.com/dotnet/designs Thanks. |
Maybe it could be considered below solution for testing purpose only: public class GeoLocationServiceForTest : GeoLocationService, IGeoLocationService |
I ended up using @richardszalay's MockHttp. Ok, I can live with a no-interfaced I use NSubstitute, and this doesn't work: var httpMessageHandler =
Substitute.For<HttpMessageHandler>(); // cannot be mocked, not virtual nor interfaced
httpMessageHandler.SendAsync(message, cancellationToken)
.Return(whatever); // doesn't compile, it's protected
var httpClient = new HttpClient(httpMessageHandler) Really disappointing. |
System.Net.Http
has now been uploaded to the repo 😄 🎉 🎈Whenever I've used this in some service, it works great but makes it hard to unit test => my unit tests don't want to actually ever hit that real end point.
Ages ago, I asked @davidfowl what should we do? I hoping I paraphrase and don't misquote him - but he suggested that I need to fake up a message handler (ie.
HttpClientHandler
), wire that up, etc.As such, I ended up making a helper library called HttpClient.Helpers to help me run my unit tests.
So this works ... but it feels very messy and .. complicated. I'm sure I'm not the first person that needs to make sure my unit tests don't do a real call to an external service.
Is there an easier way? Like .. can't we just have an
IHttpClient
interface and we can inject that into our service?The text was updated successfully, but these errors were encountered: