Replies: 1 comment 2 replies
-
We also mock test the FhirClient. I suggest you take a look at FhirClientMockTests.cs and SubstituteBuilder.cs. Note that we switched to nSubstitute instead of Moq |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've recently started working on tests for a new project, and a part of that is mocking FhirClient behavior. Now in the project I'm working on, a FhirClient is instantiated and used to make several calls as part of logic to acquire some information (for example, an encounter is used to gather charge items, which are then used to get practitioners).
I would like to write a unit test for the happy path of these methods, but I'm running into issues when mocking.
For example, in this method, the happy path involves 3 calls to the FhirClient.SearchAsync<> method with 3 different types. Because FhirClient does not have an interface, I cannot mock these calls. But I can mock an HttpClientHandler and send it into my FhirClient! However, now, I need to mock 3 separate SendAsync's within the HttpClientHandler, because each of those 3 calls is going to return something different. I don't know how FhirClient arranges my call from SearchAsync to SendAsync (I could find out by reading the source code, but that seems to go against testing third party libraries).
At the moment, I end up with something like this, which works because I'm guessing at the implementation of FhirClient.
One could argue that this means that these methods need to be refactored, but I imagine there's a lot of legacy code out there that can't just change that, so I can only think of 3 options.
Any thoughts or comments on this design decision would be appreciated, I am generally confused what is the right way to go about this, or even think about this problem.
Beta Was this translation helpful? Give feedback.
All reactions