I've been trying to figure out a way to test the following piece of code (in Elixir):
defmacro __using__(_) do
quote do
# API functions will be used from this client
import Client.API
def list_storages do
case list_buckets do
{:ok, res} ->
case res.status_code do
200 ->
res.body
|> Friendly.find("name")
|> Enum.map(fn bucket -> bucket.text end)
_ ->
res |> show_error_message_and_code
end
{:error, reason} ->
parse_http_error reason
end
end
...
The problem is the list_buckets function is being import from the Client.API module (that's already being tested in another project which I can't really change anything there). My idea was to stub/mock/dummy the API functions so that they return just a dummy reply. I've tried using defoverridable to override the list_buckets function but that doesn't work since the function definition is happening in another module.
I've read the following post by José Valim and that has helped test the Client.API module but I don't find a way to apply those concepts to this particular problem.
My only (and stupid) idea so far is to just reimplement every function inside the macro in a test file and use a dummy API function defined there but that feels very wrong and not helpful if there are code changes in the non-testing code.
Basically I want to test if the three possible cases are correct:
- Receiving
{:ok, res}and code200-> Outputs the correct data - Receiving
{:ok, res}and another code -> Output the error message and code - Receiving
{:error, reason}-> parses the HTTP error and outputs the reason for failure
Can anyone help me with this?
Aucun commentaire:
Enregistrer un commentaire