How to test a compile error?

You can eval/compile files at runtime. Just build a fixture and test against it.
You should change raise "invalid" to something a bit more helpful tho. maybe:
raise CompileError, description: "missing attribute", file: __ENV__.file, line: __ENV__.line

defmodule MustBeValidTest do
   @fixture_file Path.join(["fixtures", "must_be_valid.ex"]
   @external_resource @fixture_file

   use ExUnit.Case

   test "CompileError" do
      assert_raise CompileError, "Some Helpful Info", fn() -> 
         Code.eval_file(@fixture_file)
      end
   end
end
3 Likes