stripe / stripe-go

Go library for the Stripe API.
https://stripe.com
MIT License
2.05k stars 449 forks source link

Create A Mock Test Client #1193

Closed meggieveggie closed 1 year ago

meggieveggie commented 3 years ago

Proposed Feature

After implementing the Golang SDK there could possibly be a better test suite that can be created. For instance the current issue I am having is mocking a response from:

stripeClient.Invoices.List(params)

My thinking here would be to approach this how kubernetes has with their fake client. I understand there are many ways to mock out responses (This could definitely be documented better). However it seems that having the ability to do something along the lines of:

stripeClient := &stripeFakeClient{}

stripeClient.Invoices.Create(*Invoices{//Some Random Invoice Data})

resp := stripeClient.Invoices.List(params)

without actually calling the API would make integration with your platform a lot more seamless and speed up the process.

remi-stripe commented 3 years ago

@meggieveggie Thanks for the feedback. We usually recommend using stripe-mock for something like this which is what we use in our test suite. You can read more about this here: https://github.com/stripe/stripe-mock/ Would that work for you?

meggieveggie commented 3 years ago

@remi-stripe My understanding of stripe-mock is that it is specifically used for integration tests and not unit tests. If this is not the case then I will try and use stripe-mock

ianjabour-stripe commented 3 years ago

@meggieveggie thank you for this suggestion. You are correct in that stripe-mock is built mainly for integration tests, but it should still allow you to quickly test your integration without actually hitting the Stripe API.

The limitation with stripe-mock is that it is stateless, so fetching objects you create in a test will not work. Also, the data made available is limited to that which we generate based off resource fixtures from within our API.

We have been experimenting with ways around these limitations, but don't have anything readily available right now.

saniales commented 2 years ago

to everybody still searching for a solution for unit tests without stripe mock

I managed to mock without it in the way I explained here on Stack Overflow

guptaaashutosh commented 3 months ago

@meggieveggie Thanks for the feedback. We usually recommend using stripe-mock for something like this which is what we use in our test suite. You can read more about this here: https://github.com/stripe/stripe-mock/ Would that work for you?

how it can be use in unit testing?. Can you refer any example