Mix test cover not covering macros in web.ex

Hey guys, I’ve been writing tests for a phoenix project that I’ve been working on. Right now I’m writing tests for my view and it shows poor coverage on the view macro in my web.ex
image
I suspected maybe it’s because the Phoenix Controller imported inside the view macro in web.ex (I’m wrong mostly) causing this. So I wrote a test like this


This is the function definition for the view macro

My phoenix version is 1.5.14

Thanks!

This is a well-known issue, it’s related to code generation and the solution usually is to ignore this kind of file from your coverage calculations. Maybe there are better ways, but I’m not sure as I don’t rely on coverage for my projects.

Imagine you have the following scenario:

defmodule Phoenix.View do
  quote do
    def special_view_function() do
     ...
    end
  end
end

defmodule MyView do
  use Phoenix.View
end

Without going into many details on how this works, after first compilation (macro expansion) your MyView module will become:

defmodule MyView do
  # This is exactly the same code defined in Phoenix.View
  def special_view_function() do
  ...
  end
end

You have now the special_view_function/0 generated in your module, obviously when the coverage tool will run, it will see that and point to it. Testing these functions makes no sense, as in a lot of cases they are internal behavior that is used by the framework to operate.

1 Like

Yes, this makes total sense. Just wanted to know if I missed out on anything.

1 Like