JoeZMar
I am building an app that uses Stripe for payments. I’ve read about people’s opinion of mocking when it comes to Elixir, but I also know that having actual API calls within your test suite slow your tests down and also increase how many times the API is called if there is a limit. Is testing more there to ensure that changes within your app don’t break when refactoring? Or is it there to help ensure both my app and the API are working together? For example, I could create a mock server that acts as the API in the test environment. The mock server could respond to the same routes as the API, but with a mocked response. If the API ever changes the test won’t break, but if my application makes changes that break the current API implementation it would catch it.
So the question is, how do most people write test for modules that involve other APIs. My other thought was to create two separate test modules. One module that I have a specific @tag meant to skip the module during mix test but it will actually send request using our test secret key for Stripe to ensure proper integration. But still create a mock server that will run every time we run our tests.
Please let me know your opinion.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 10 of 17 Posts
gregvaughn
I work on a project that is very much an integration layer between many 3rd party APIs (20-ish including stripe). All of our automated tests go through mocks in some form or fashion. Depending on how “old” a particular integration is we might use the hex package bypass in some cases, but we also have a home-built global mocking framework that prevents async tests and is based upon indirection in the Application.env. Lately we’ve moved to a struct-based command/token approach which I have discussed a bit publicly (GitHub - gvaughn/quest: Pattern for building API clients in Elixir · GitHub).
In any case, we do not have automated tests that actually hit 3rd party APIs. We’re prepared to deal with a temporary production outage if a 3rd party changes their responses significantly. But if that is a high concern of yours, yes, I’d use an ExUnit @tag to manage when it is executed.
Fl4m3Ph03n1x
There is no silver bullet. I have a very personal take on testing, some people agree others don’t. What I can say in your case, is that you shouldn’t make your application dependent on Stripe, you should have a connector, a contract, and then have the Stripe client implement that contract. At least, this is the original idea Jose Valim has about testing in Elixir: Mocks and explicit contracts « Plataformatec Blog
Thus, I believe the community would suggest something among the lines of:
Me personally, I prefer constructor dependency injection. When I create my entities in Elixir, I simply pass them the set of functions they are going to use (I inject the dependencies). Then when I need to test, I simply pass dummy functions as dependencies, or better, I pass stubs and spies to make sure I am performing the external calls as I am supposed to.
This goes in line with behavioral tessting, some people don’t like it because it couples your test to your implementation a little bit more, but I find this is the best way to ensure every unit works as intended. As usual, YMMV.
LostKobrakai
I’ll add a third bullet point to your list:
…
3. Test implementations against the contract (if possible). This might include mock implementations as well.
If both stripe and a mock implementation behave the same one can confidently use the mock for all tests, which don’t specifically involve testing the behavioural contract of the real implementation.
OvermindDL1
This is known as the Witness pattern in Functional languages.
Fl4m3Ph03n1x
Can you link me to some articles about that? I only found links about the gospell and religion on Google
I took this from an article written by Martin Fowler, where he defines 3 types of dependency injection. Constructor injection is one of them:
cnck1387
Totally unrelated but do you happen to have that Gateway module open sourced somewhere? I’m also building a system that will interface with multiple payment gateways and would be curious to see how you implemented it.
OvermindDL1
Lol, yeah Google isn’t very good about searching for programming stuff, DuckDuckGo is actually a LOT better for searching for programming related terms, it even has whole search modes for it that Google doesn’t have.
The most basic, direct, and unreadable thing would be from Haskells wiki:
Type witness - HaskellWiki
Even OCaml has an example of them in the GADT section of the official spec: https://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec256
But in an Elixir world it would be like:
Or in a module form:
In essence a witness is just a type or action that depends on a type.
Typeclasses, like in Haskell, are a non-generic form of witnesses. Like take this function in Haskell:
The
Num ais a typeclass, if you call this function likeadd 1 2it will return3and if you call it likeadd 1.0 2.0it will return3.0, for any type that fulfills the typeclassNum 'a. However, look at the=>, that’s just a special operator in Haskell that means to ‘auto-fill what comes before’, let’s turn that back into a->:Now you have to call it like
add (Num Int) 1 2to return 3, thatNum Intis the witness, the typeclass is essentially a module reified on that specific type based on the typeclass definition of it, and it passes that module in that location (it’s actually a record in Haskell, but whatever).In other words, a witness just allows you pass an action over some other type into a function. Haskell’s typeclasses make it “baked in”, in that you can define a witness globally and it can be used globally, but you can’t change it, it is what it is defined as, where if it is something you have to pass in manually, as the OCaml ecosystem does, then you can change it at will, which makes, for example, mocking it absolutely trivial.
Passing in a module or a set of functions into something to operate on a value, whether also passed in or existing entirely internally, but is specified and handled by those functions, makes those functions/module a witness.
In Elixir, I keep my work program very segmented as lots of small dependencies, but they all share the main app’s Repo instead of their own by me defining the
MyServer.Repomodule in the global config for each dependency, they then access it just viaApplication.get_env/2each time they need it (via a default option on function args, but close enough). In other words I am passing in a witness, the Repo, to ‘witness’ or operator over the data that is being processed, I.E. t he schemas and changesets. This is a pattern I use, probably excessively, because of my OCaml history (super common pattern there), and consequently it makes it sooo easy to ‘mock’ things without needing to grab a code generator like Mox or so.As an addition, I heavily follow the pattern of almost every function taking a set of optional named arguments, like:
Where
get_repois essentially this defined fairly globally included:So I can define a repo globally for a dependency, or I can redefine it on a function-by-function basis, etc… Regardless, it’s just a witness being passed in ‘somehow’ that the functions use to perform work. It’s an exceptionally old pattern in ML languages.
gregvaughn
I don’t. What we use is quite project specific, plus if I were writing it today, I’d do it differently.
It’s a GenServer with a custom module we
useto avoid some boilerplate. We use an erlang library calledthrottlewhich is a set of mnesia based counters so we can manage our outgoing rate limits. I like that and would reuse it.We also use poolboy so we can limit the outgoing concurrency per service. If/when I revisit it, I’ll likely look at either
parentorGenStageinstead to manage this feature. This may be unnecessary in many applications, but it is baked into all of our Gateways.The core client function exposed is
SomeServiceGateway.dispatch(%Quest{} = q)which checks rate and concurrency limits, then ultimately callsQuest.dispatch(q)and logs errors and usesstatixto send stats to Datadog. There’s retry logic for some status codes in some cases too.I don’t want to hijack this thread any more, so feel free to DM or start a new thread if you want to discuss more.
fireproofsocks
This post is a bit dated, but holy smokes, this problem never gets old (full disclosure: perhaps not everyone shares my zeal for tests).
To date, the best pattern I have found to really (really) test an app which relies on 3rd party services is simply to make good use of Elixir’s
Application.get_env/2andApplication.put_env/3. It ends up working a lot like “service locators” in other languages (but I confess, the cleanest implementation I’ve seen of this personally was in PHP, either with the unfortunately named Pimple package or in the popular Laravel framework).In a nutshell, you might take code like this:
and refactor it to something like this:
You should probably avoid
Application.get_env/3and instead explicitly list these 3rd party modules in your config somewhere because that will make the following pattern in your tests easier. In your tests, you can do this:This
setupwill ensure that the normally configured values always put back in place after a test. Why? Because in your tests, you can override those modules viaApplication.put_env/3.The pattern I use is that I create some “mock” modules with the same function names as the original module (yes, this is kind of an interface, but since they are 3rd party modules, there’s no guarantee that they defined a behaviour). The mocked functions return values I captured when I was testing it for real. I might have a couple mocks for each module: one for each response that I need to test (e.g. success, failure, some other failure, etc).
My tests might end up looking something like this:
Hopefully that strategy makes sense. An Elixir application does have some state with its config, and you can manipulate it via
Application.put_env/3.I still test the real 3rd party module, but I use a tag that I’ve configured to skip, e.g.
@tag :external– mytest_helper.exshas something like this:And my test might look like:
OvermindDL1
@fireproofsocks That’s exactly the pattern I use. I’m a huge fan of ‘naming’ call modules in configs, it makes it so easy to test or swap out or wrap or whatever as needed.