Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pry while testing

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?

like image 701
Dmitry Avatar asked Apr 16 '15 09:04

Dmitry


People also ask

How does pry work?

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.

How do I debug with pry?

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.

How do I stop binding on pry?

To exit everything, use: exit! This should ignore all proceeding bindings. This also kills the server at the same time.

How does binding pry work?

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.


1 Answers

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 
like image 126
Paweł Obrok Avatar answered Sep 18 '22 23:09

Paweł Obrok