nothingworksinc / ticketbeast

Back to the lesson videos:
https://course.testdrivenlaravel.com/lessons
553 stars 10 forks source link

[Don't Mock What You Don't Own] - How to API test without sandbox environment? #80

Closed debasishrowlo closed 2 years ago

debasishrowlo commented 6 years ago

How do we integration test external APIs which do not provide Test mode like Stripe does (e.g - Wordpress or Skype API)?

gMagicScott commented 6 years ago

For WordPress, I installed it as a fixture in my tests. You can shell out from php to hit wpcli to migrate that database. I added a MU plugin to the be able to direct access the information I needed for assertions.

For other services, if it's free and has side effects you can work with, make a testing account.

Now I'm trying work with OFX (banking). Still not totally sure how I want to approach that. Real money can be lost if you screw up.

jkniest commented 6 years ago

I had the same with discord. In the end I've just created another "bot" and a custom server for testing since it does not cost anything on discord.

That's why I like services like stripe and braintree so much..

MrRio commented 6 years ago

Use PHP VCR! https://github.com/php-vcr/php-vcr

jkniest commented 6 years ago

Sure, this would work but you wouldn't have the 100% confidence that it will really work. In the end what I did was:

mattkoch614 commented 4 years ago

I had the same question as I was watching module 9. @adamwathan Can you comment here? We're running into a situation where we have an API we need to integrate with that does not have any concept of test/sandbox tokens - would you fall back to mocking in that scenario or use a different approach?

I'm assuming the answer lies somewhere along the lines of: https://adamwathan.me/2016/01/25/writing-your-own-test-doubles/ :)

adamwathan commented 4 years ago

Sorry for the delay — in that case I would still just test my app code against a fake of my own API wrapper for the service. The only real difference is that you can't actually integration test your wrapper, so you just have to live with some risk there. I'd keep the wrapper as simple and unclever as possible so that it rarely needs to be changed and any time that code does change I'd test the crap out of it manually.