How to properly mock an API in testing? #393
-
Hey all, I'm really liking this template, but I'm right now struggling on how to best mock an external API my app is using for testing. In my current mindset I would need to define the mock-method used in almost every testcase differently (testing if appropriate data is returned from the API, testing if nothing is returned from the API ...), but how do I register this specific mock with the DI so that the mediator handler is going to use it? I hope that my problem is understandable. If my current though-process on how to tackle this is wrong than I'm more than happy to learn. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I've extended https://gist.github.com/ronnieholm/7c97b09097c44a3c33a7f36c0d053fff |
Beta Was this translation helpful? Give feedback.
-
Thanks for the fast answer, @ronnieholm ! I too thought about your solution to this problem, but wouldn't this recreate the entire DI container for each test? Is this something to even worry about? |
Beta Was this translation helpful? Give feedback.
I've extended
TestBase.cs
andTesting.cs
. Here's an example of mockingIEmailSender
:https://gist.github.com/ronnieholm/7c97b09097c44a3c33a7f36c0d053fff