I’m trying to debug interactively with Kernel.dbg/0
in IEx. But when I step through the code with n
(next
) or continue
, this ultimately changes my function return value. What am I doing wrong?
Sample code:
defmodule DebugTest do
use ExUnit.Case
describe "test debug" do
test "test a pipeline with dbg()" do
assert double_add([1, 2, 3, 4]) == 20
end
end
defp double_add(numbers) do
numbers
|> Enum.map(&(&1 * 2))
|> Enum.reduce(&+/2)
|> dbg()
end
end
When I run this with iex -S mix test test/debug_test.exs
, execution is halted, dbg
does its work and prints each pipeline step, but after leaving the double_add
function (with next
or continue
), my test fails:
1) test test debug test a pipeline with dbg() (DebugTest)
test/debug_test.exs:5
Assertion with == failed
code: assert double_add([1, 2, 3, 4]) == 20
left: :ok
right: 20
stacktrace:
test/debug_test.exs:6: (test)
Using dbg
changed the function return value to :ok
.
When I use IEx.pry()
instead of dbg()
, this doesn’t happen, but of course I don’t get the niceties of dbg
anymore:
defp double_add(numbers) do
require IEx
IEx.pry()
numbers
|> Enum.map(&(&1 * 2))
|> Enum.reduce(&+/2)
end
How is dbg()
meant to be used interactively?