cah4a / dart_nock

HTTP requests mocking library for dart and flutter.
https://pub.dev/packages/nock
MIT License
20 stars 14 forks source link

Question: is it possible to not mock certain URLs? #10

Closed jseminck closed 3 years ago

jseminck commented 3 years ago

Hello, first of all apologies as this is not necessarily an issue but more of a question that I could not figure out from the docs or API reference. Is it possible to mock only certain URLs and not others?

For example, mock http://localhost:8080 urls, but do not mock URLs to https://flagcdn.com/ or some similar CDN?

Thanks

cah4a commented 3 years ago

Hey, @jseminck

No, currently nock doesn't support performing real HTTP requests. Any request that didn't mock will fail the test using NetConnectionNotAllowed error.

I don't know whether it is a good idea, cuz in this case the tests will test much more than the code: internet connection, DNS, SSL, remote servers, response content, etc...

Reproducibility is a central tenet of testing. So I'm trying to avoid testing a real API that belongs to me and, of course, don't want to test some API that doesn't.

jseminck commented 3 years ago

I agree with you, and I managed to find another workaround to my problem.

Just for context, we are using https://pub.dev/packages/cached_network_image to get flags from https://flagcdn.com/ and this package throws exceptions in case an invalid URL is given to it. I didn't want to mock out all the flags as that seems kind of silly. My other best solution was to create a wrapper widget around CachedNetworkImage that returns a SizedBox() when running the test.

Thanks for the reply!

cah4a commented 3 years ago

Yeah, I bet this is the best solution.

Thanks for sharing your experience. It could be useful for others who face similar issues.