I'm pretty new in Elixir, but have a lot fun with it!
I came from Ruby world, so start looking analogy. And there is exist debugging tool pry
. Using binding.pry
I can interrupt any session. I found something similar in Elixir – IEx.pry
. But it doesn't work when I'm testing something through ExUnit
.
Question – is it is possible to interrupt testing session and run iex with current environment?
Pry is like IRB on steroids Both IRB and Pry use REPL commands: Read, Evaluate, Print, and Loop. But Pry allows you to go further when debugging. For example, Pry gives you color-coded syntax, which helps when you're trying to figure out what will happen when code is executed.
Invoking pry debugging To invoke the debugger, place binding. pry somewhere in your code. When the Ruby interpreter hits that code, execution stops, and you can type in commands to debug the state of the program.
To exit everything, use: exit! This should ignore all proceeding bindings. This also kills the server at the same time.
So when you place the line binding. pry in your code, that line will get interpreted at runtime (as your program is executed). When the interpreter hits that line, your program will actually freeze and your terminal will turn into a REPL that exists right in the middle of your program, wherever you added the binding.
You need to start your tests inside an iex session - you can do that by running iex -S mix test
. Then you can use IEx.pry
inside your test:
require IEx test "the truth" do one = 1 IEx.pry assert one + one == 2 end
You'll be asked if you want to allow prying into the session:
Request to pry #PID<0.143.0> at test/test_app_test.exs:7. Allow? [Yn]
And all the context at that point will be available to you:
pry(1)> one 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With