gjaldon / ecto_enum

Ecto extension to support enums in models
MIT License
562 stars 131 forks source link

how to test and pass coverage? #85

Closed silva96 closed 5 years ago

silva96 commented 5 years ago

I have the following ecto_enums.ex file

import EctoEnum
defenum PostStatusEnum, :post_status, [:importing, :imported]
defmodule MyApp.EctoEnumsTest do
  use MyApp.DataCase

  describe "PostStatusEnum" do
    alias MyApp.MyContext.Post

    test "raises when input is not in the enum map" do
      error = {:status, {"is invalid", [type: PostStatusEnum, validation: :cast]}}
      changeset = Ecto.Changeset.cast(%Post{}, %{"status" => "a_not_valid_status"}, [:status])
      assert error in changeset.errors
    end

    test "doesn't raise when input is in the enum map" do
      changeset = Ecto.Changeset.cast(%Post{}, %{"status" => "importing"}, [:status])
      assert length(changeset.errors) == 0
      changeset = Ecto.Changeset.cast(%Post{}, %{"status" => :importing}, [:status])
      assert length(changeset.errors) == 0
    end
  end
end

Tests pass, im happy with that. But coverage reporting tool is showing 0% for ecto_enums.ex

Finished in 1.8 seconds
21 tests, 0 failures

Randomized with seed 18593
----------------
COV    FILE                                        LINES RELEVANT   MISSED
  0.0% lib/my_app/ecto_enums.ex                    2        1        1

How can I make coverage tool put a 100% in my ecto_enums.ex ?

silva96 commented 5 years ago

Auto responding myself

test "it has the correct map values" do
    assert PostStatusEnum.__enum_map__() == [importing: :importing, imported: :imported]
end