Skip to content

Commit

Permalink
Add more accurate error message when an unexpected exception is raised
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-dohm committed Sep 20, 2017
1 parent 7abbed8 commit 63d1806
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/espec/assertions/raise_exception.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ defmodule ESpec.Assertions.RaiseException do

defp error_message(subject, [module], err_module, positive) do
if positive do
"Expected #{inspect subject} to raise the `#{module}` exception, but nothing was raised."
case err_module do
{false, nil} -> "Expected #{inspect subject} to raise the `#{module}` exception, but nothing was raised."
err_module -> "Expected #{inspect subject} to raise the `#{module}` exception, but `#{err_module}` was raised instead."
end
else
"Expected #{inspect subject} not to raise the `#{module}` exception, but the `#{err_module}` exception was raised."
end
Expand Down
10 changes: 10 additions & 0 deletions spec/assertions/raise_exception_spec.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule ESpec.Assertions.RaiseExceptionSpec do
let :func1, do: fn -> raise(ArithmeticError) end
let :func2, do: fn -> 1 + 1 end
let :func3, do: fn -> List.first(:a) end
let :func4, do: fn -> raise(ArgumentError) end

context "Success" do
it "checks success with `to`" do
Expand Down Expand Up @@ -67,6 +68,15 @@ defmodule ESpec.Assertions.RaiseExceptionSpec do
end
end

it "checks error with `to`" do
try do
expect(func4()).to raise_exception(ArithmeticError)
rescue
error in [ESpec.AssertionError] ->
expect(error.message) |> to(end_with "to raise the `Elixir.ArithmeticError` exception, but `Elixir.ArgumentError` was raised instead.")
end
end

it "checks error with `to`" do
try do
expect(func2()).to raise_exception(ArithmeticError, "bad argument in arithmetic expression")
Expand Down

0 comments on commit 63d1806

Please sign in to comment.