How to test view modules?

Let’s say I’ve got the function below in my app/lib/app_web/views/project_view.ex

  def find_if_checked(nil, _, _), do: ""

  def find_if_checked(map, type, key) do
    with types when not is_nil(types) <- map[key],
         true <- type in Map.keys(types) do
      "checked"
    else
      _ ->
        ""
    end
  end

How do I test functions in view modules in general? Do I just mock the map with necessary fields and pass it to the function in the test, or do I prepare & use the real map/data structure for testing?

There seems to be nothing specific to the fact that this is a view function. If you mock the map, you’re tying it to the implementation. If the implementation changes, the test will need to change. That’s why I prefer to test with real data.

2 Likes