I have a similaire scenario:
int retrievePrice(string productName) {
string id = restApi.get("productid", productName);
return restApi.get("productprice", id);
}
The restApi
could also be a third party. In order to make the test self sufficient, I end up mocking the restApi
.
If I directly test this method, I will end up testing the mock and my test will be coupled with the production code. Thus, any refactor will be painful.
Should I only test a higher level function? If it is the case, should I mock restApi
or the whole retrievePrice
function? It is ok to leave this method untested?
Edit:
- The function under test doesn't have a side effect, thus I don't need a spy to test the logic of this function.
- Outside of the short/mid term benefice of having tests, I am more concerned about the long term. The day when I will need to change the implementation (without breaking the contact), I expect my tests to still pass. Maybe it is a mocking framework problem (mock on demand vs fully mock a class).
int?
. That said, if you already have a way to easily test against a mock API, adding the relevant tests here would be simple and probably worth it.