DEV Community

Discussion on: External Service testing in Phoenix

Collapse
 
rhymes profile image
rhymes • Edited

Hi Vincent, what about using something like exvcr ?

I have no knowledge or experience in Elixir but I often use a "vcr" library with other languages. It allows me to actually hit the server at least once (following requests are mocked) and you can also refresh all the interactions from time to time to make sure that the real server is responding with the expected data.

I obviously agree that trusting the Elixir S3 library for the response is a good ideae but this way you can also make sure the HTTP traffic is consistent

Collapse
 
vinhnglx profile image
Vincent Nguyen

I heard about exvcr, it's another approach - can work but technically, I don't want to hit the server and ‘replay’ it back during tests.

From the awesome thoughtbot post, there're considerations when using VCR:

  • Communication on how cassettes are shared with other developers.
  • Needs the external service to be available for the first test run
  • Difficult to simulate errors

I also realized the mocking module looks very simple, I should handle multiple response statuses.

Collapse
 
rhymes profile image
rhymes

I do mock errors sometimes or I just call the server with invalid data and register the possible error messages.

Some APIs have different error messages for different cases and for me it's faster to record them than to actually figure out how to mock all of them.

My issue with API mocks is that they can be difficult to maintain updated, it's not this case because S3 is not going to change at all but for APIs where there's no client, just the documentation, I am way faster if I record it once instead of using mocks.

To be able to mock those, you still need to wire up Postman, hit them once, check the response, mock such response, so why not do that using VCR?

Thread Thread
 
vinhnglx profile image
Vincent Nguyen

I understand and agree with you. But I don't want to use VCR because there are many ways to do the external testing. VCR is just an approach.

About the reason: after reading the thoughtbot post and personally I want to keep my code is simple. That's all.

Btw I can use Postman to get the response from external services and update for my mock modules.

Thread Thread
 
rhymes profile image
rhymes

Btw I can use Postman to get the response from external services and update for my mock modules.

that is absolutely true :-)

Thanks for the discussion!

Collapse
 
seanwash profile image
Sean Washington

I've found the Bypass lib to be very useful: github.com/PSPDFKit-labs/bypass

Here are a couple resources I used to get started with it: