Fl4m3Ph03n1x
Background
I have library that uses HTTPoison for some functionality I need to test. To achieve this I am using Mox, which I believe is the universal mocking library for Elixir (even though there are others this one has the seal of approval of José Valim)
Problem
Everything is going fine, I define my mocks in the test_helpers.exs:
ExUnit.start()
Mox.defmock(HTTPoisonMock, for: HTTPoison)
And I set up my dummy tests:
defmodule Alfred.Test.Http.Test do
use ExUnit.Case, async: true
import Mox
# Make sure mocks are verified when the test exits
setup :verify_on_exit!
describe "get" do
test "works on OK" do
HTTPoisonMock
|> get(:get, fn _ -> 1 end)
assert HTTPoisonMock.get(1) == 1
end
end
end
The problem here is I can’t run them:
module HTTPoison is not a behaviour, please pass a behaviour to :for
Mock Contracts, not implementations
Now, I know José Valim supports this ideology, thus everything we should mock should have a contract. But HTTPoison is not mine and it doesn’t have one. So this brings me to the following question:
- How can I mock 3rd libraries that don’t offer behaviours using Mox ?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
bonf
You can wrap HTTPoison with your own module.
This works for me:
define a behaviour
define the real implementation
in config/test.exs
for dev/prod
usage:
in your test:
Hope this helps.
Fl4m3Ph03n1x
I see your solution uses different implementations depending on the MIX_ENV. While not completely wrong, I would like my tests to be independent from which ENV they run on.
This is actually one of the issues Mox tries to solve:
bonf
I think this is exactly what the article is suggesting.
quoting from the article
Fl4m3Ph03n1x
It would appear you are correct!
Sorry for the misdirection on my part!
peerreynders
Interestingly that article links to a google groups topic:
easco
I second @peerreynders’ call-out. You should not mock
HTTPoison, but mock the higher level module that is usingHTTPoisonto get data. For example, if you useHTTPoisonto call an HTTP API returning the temperature outside, create a module representing the weather service, with a function for getting the current temperature, and mock the weather service module.Fl4m3Ph03n1x
I am afraid I disagree with this. I don’t want to test the higher module that uses HTTPoison. I know it works. I have a suite of tests for that. What i need to know is if this module, let’s call it,
TemperatureStatsrespects the contract HTTPoison uses. The only way to do this, to know this, is to mock HTTPoison and to make sure I am calling it’s functions with the correct parameters, i.e., that I am invoking HTTPoison with the correct headers, URL, etc.If there is a better way to do this, I don’t know.
@peerreynders If your test suite doesn’t break when you change HTTP client (and by default, when you change the contract the HTTP client is using) then it means you are not testing it at all.
Is this bad? good? Some people defend we shouldn’t test these things leave them to QA or for a complete suite of slow integration tests. I can see the argument there, but in my experience, when you leave something for someonese else to test another time, reality dictates that nothing gets actually done, and so in the end the real testers will be your users. To me this is not acceptable, so I unit test everything.
peerreynders
I think there is a bit of misunderstanding here. The intent that I see behind that statement is that the HTTP client library is treated as an implementation detail. So there really are two approaches:
When testing, simply leave the HTTP client library in place. In a way this approach is similar to testing against a working, running database.
Establish a boundary between your code and the HTTP client library. I.e you design the contract while being constrained by the capabilities of the library.
Ultimately the second approach is consistent with Rainsberger’s “Narrowing API/Pattern of Usage API”. So in that case you would have your own HTTP client behaviour module which acts as the interface for the Happy Zone while the callback module interfaces with HTTPPoison in the DMZ to complete the DMZ adapter.
For your micro tests you use a faux callback module when performing collaboration tests. Much less frequently you run contract tests with the real callback module and HTTP client library to show that the contract implementation is still behaving in the expected way (Clearing Up the Integrated Tests Scam).
So now we’re just back at to the post that you’ve already marked as the solution.
sgyyz
As you can see
HTTPoisonit usesHTTPoison.Basedirectly. AndHTTPoison.Basedefines behaviors. Which means we can mockHTTPoison.Baseinstead of mockHTTPoison.Reference: