BitGonzo

BitGonzo

I’ve started to test my application and immediately run into the mock debate. Just done reading:

The above suggests using explicit contracts and config variables to manage environment-specific dependencies initially, and then goes on to suggest the simpler method of injecting dependency directly.

However, my current method is a little different. I am using explicit contracts and delegation such as:

config :myapp, request_client: MyApp.Tesla.Request

defmodule MyApp.Request do
  @client Application.get_env(:myapp, :request_client)

  defdelegate make, to: @client

  @callback make(etc) :: etc
end

defmodule MyApp.Tesla.Request do
  @behaviour MyApp.Request

  use Tesla
  plug Tesla.Middleware.Retry, delay: 1000, max_retries:: 3

  def make do
    # perform request, return response
  end
end

This gives me a common interface and call MyApp.Request.make, while also giving me the ability to switch out the client with configuration, which I will use during testing.

Does this look sensible? Is there something inherently wrong with the above approach or, in your opinion, an improvement?

The second question is about testing MyApp.Tesla.Request – perhaps I can switch out the client I use in testing, but surely I still need to test that my Tesla request is configured/running properly.

Showing Posts 1 to 10

AmberBit

AmberBit

This looks okay. You can also use mox that basically does the same thing + has verifications/assertions you can use in tests.

BitGonzo

BitGonzo OP

Was just reading through mox documentation then. I’ll see how far I get in my tests.

hubertlepicki

hubertlepicki

Maybe examples here will make it a bit more clear: Mox

BitGonzo

BitGonzo OP

Not entirely sure how mox is working, but I have this:

defmodule Example do
  @callback function(integer()) :: integer()
  @callback another(integer()) :: integer()
end

Mox.defmock(ExampleMock, for: Example)

test "adheres to contract" do
  ExampleMock
  |> expect(:function, fn x -> x end)

  assert ExampleMock.function("string") == "string"
end

..passes. But my type specifies integer and I have another function defined in my @callback which hasn’t been defined in the ExampleMock (though those guarantees don’t appear to exist anyway when using @behaviour in derived modules). Are those types (integer(), etc) just for documentation purposes? They don’t have any effect beyond that?

Seems a shame to have all of this ceremony without the guarantees. Seem more like guidelines than explicit contracts?

idi527

idi527

I think they can be checked by dialyzer.

BitGonzo

BitGonzo OP

Ahh I see. I’ve not delved into that yet.

hubertlepicki

hubertlepicki

Yep, these are not checked by Elixir compiler, so serve as documentation only unless you use dialyzer.

BitGonzo

BitGonzo OP

Fair enough. I’d like to use dialyzer but that’s a whole can of worms I’m unwilling to open. Included it in a project and it’s coming up with all kinds of non-issues/noise, even with warnings disabled. Not enough time in the day to be fighting with another tool :stuck_out_tongue:

@spec is good enough for me.

kokolegorille

kokolegorille

It is not that complicate to use dialyzer with dialyxir… just add

{:dialyxir, "~> 0.5.1", only: [:dev], runtime: false}

to your deps.

Then run (it take some time at first, but then it’s ok)

$ mix deps.get
$ mix dialyzer

I had to ignore some warnings coming from some Erlang sources included in my project. To configure this is also simple, just add to mix.exs, in the project section.

      dialyzer: [
        ignore_warnings: "dialyzer.ignore-warnings"
      ],

and create a file dialyzer.ignore-warnings containing the rules You want to ignore.

BitGonzo

BitGonzo OP

Yep, that’s what I tried.

I just can’t be doing with cryptic stuff like the following about code which works:

`

The pattern {'ok', Vresponse@1} can never match the type #{'__client__':=fun(), '__module__':=atom(), '__struct__':='Elixir.Tesla.Env', 'body':=_, 'headers':=#{binary()=>binary()}, 'method':='delete' | 'get' | 'head' | 'options' | 'patch' | 'post' | 'put' | 'trace', 'opts':=[any()], 'query':=[{atom() | binary(),binary() | [{atom() | binary(),binary() | [{_,_}]}]}], 'status':=integer(), 'url':=binary()}
`

Well it can match the type because it does. I would love to have this checking if it worked.

Also:

:0: Unknown function 'Elixir.GenStage':start_link/2 :0: Unknown function 'Elixir.Request':make/1 lib/gen_stage.ex:1: Callback info about the 'Elixir.GenStage' behaviour is not available

..and in my templates (using slime):

/templates/layout/app.html.slim:1: Guard test _@7::bitstring() =:= 'false' can never succeed

Where Next? Top

Trending in Questions Top

RSP87
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
RemyXRenard
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
nseaSeb
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
velrest
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
samoloth
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
FlyingNoodle
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
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews