Pry doesn't work when launched from method invoked by RSpec test - ruby

I'm trying to use Pry with RSpec.
Goal is to be able to drop binding in method and debug it.
Here is what I have.
lib/play.rb
class Play
def self.hello
print 'Hello world!'
require 'pry'; binding.pry
end
end
spec/play_spec.rb
require_relative '../lib/play'
describe Play do
it 'Play#hello should print message' do
expect {Play.hello}.to output('Hello world!').to_stdout
#require 'pry'; binding.pry
end
end
If I uncomment binding in spec file and run
rspec
I'm getting into Pry which behaves as expected
Frame number: 0/23
From: /Users/iiezhachenko/Documents/Repos/bdd-ruby- playground/spec/play_spec.rb # line 6 :
1: require_relative '../lib/play'
2:
3: describe Play do
4: it 'Play#hello should print message' do
5: expect {Play.hello}.to output('Hello world!').to_stdout
=> 6: require 'pry'; binding.pry
7: end
8: end
[1] pry(#<RSpec::ExampleGroups::Play>)> ls
RSpec::Core::MemoizedHelpers#methods: is_expected should should_not subject
RSpec::Core::Pending#methods: pending skip
RSpec::Mocks::ArgumentMatchers#methods:
If I comment out binding.pry in spec fill, put it in method which is tested and invoke "rspec" again. I'm getting into completely messed Pry.
[1] pry(Play)> ls
[2] pry(Play)> fghjk
[3] pry(Play)> kweutyalfh
[4] pry(Play)> exit
F
Failures:
1) Play Play#hello should print message
Failure/Error: expect {Play.hello}.to output('Hello world!').to_stdout
expected block to output "Hello world!" to stdout, but output "Hello world!\n\e[1mFrame number:\e[0m 0/32\n\n\e[1mFrom:\e[0m /Users/iiezhachenko/Documents/Repos/bdd-ruby-playground/lib/play.rb # line 4 Play.hello:\n\n \e[1;34m2\e[0m: \e[32mdef\e[0m \e[1;36mself\e[0m.\e[1;34mhello\e[0m\n \e[1;34m3\e[0m: print \e[31m\e[1;31m'\e[0m\e[31mHello world!\e[1;31m'\e[0m\e[31m\e[0m\n => \e[1;34m4\e[0m: require \e[31m\e[1;31m'\e[0m\e[31mpry\e[1;31m'\e[0m\e[31m\e[0m; binding.pry\n \e[1;34m5\e[0m: \e[32mend\e[0m\n\n\e[0G\e[1m\e[1;34mPlay.methods\e[0m\e[0m: hello\n\e[1m\e[1;34mlocals\e[0m\e[0m: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_\nNameError: undefined local variable or method `fghjk' for Play:Class\nfrom (pry):1:in `hello'\nNameError: undefined local variable or method `kweutyalfh' for Play:Class\nfrom (pry):2:in `hello'\n"
Diff:
## -1,2 +1,17 ##
Hello world!
+Frame number: 0/32
+
+From: /Users/iiezhachenko/Documents/Repos/bdd-ruby-playground/lib/play.rb # line 4 Play.hello:
+
+ 2: def self.hello
+ 3: print 'Hello world!'
+ => 4: require 'pry'; binding.pry
+ 5: end
+
Play.methods: hello
+locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_
+NameError: undefined local variable or method `fghjk' for Play:Class
+from (pry):1:in `hello'
+NameError: undefined local variable or method `kweutyalfh' for Play:Class
+from (pry):2:in `hello'
# ./spec/play_spec.rb:5:in `block (2 levels) in <top (required)>'
Anyone faced such issue?

As the answer by OP indicates, the problem involves console output suppression, which can inadvertently break Pry.
If you check your spec/spec_helper.rb file, there is likely some code inside the RSpec configuration block that is referring to $stderr and/or $stdout. Removing those lines of code should fix the problem.
For other examples of how this output might be getting suppressed, see the answers to this question: Suppress console output during RSpec tests

Issue resolved by removing stdout capturing from test. Thanks everyone.

Related

__FILE__ returns different value when using binding.pry

__FILE__ returns the path of the current Ruby script file.
One potentially significant problem is that, if using binding.pry, __FILE__ evaluates to (pry). It is potentially problematic to have __FILE__ evaluate to different values depending on whether it is evaluated in the context of binding.pry. For example,
$stdout.print "****************************************\n\n"
$stdout.print "FILE: #{__FILE__}\n\n"
$stdout.print "****************************************\n\n"
binding.pry
When the script pauses at binding.pry, I get:
__FILE__
# >> (pry)
Does anyone know any mechanism to get the path of the current file even in the context of binding.pry?
Use _file_ instead of __FILE__. For example, given two files:
# foo.rb
require 'pry'
require './bar'
binding.pry
b = Bar.new
and:
# bar.rb
require 'pry'
class Bar
def initialize
binding.pry
end
end
Run them with ruby foo.rb:
ruby foo.rb
From: /Users/username/foo.rb # line 3 :
1: require 'pry'
2: require './bar'
=> 3: binding.pry
4: b = Bar.new
(main):1 ⇒ _file_
=> "/Users/username/foo.rb"
(main):2 ⇒ exit
From: /Users/username/bar.rb # line 4 Bar#initialize:
3: def initialize
=> 4: binding.pry
5: end
(#<Bar:0x00007fbb6caaff08>):1 ⇒ _file_
=> "/Users/username/bar.rb"
_file_ and any other local variable names can be found in binding.local_variables.
Sergio Tulentsev made a simple suggestion, assign __FILE__ to a variable before invoking binding.pry.
anothermh, mentioned _file_ which is available in binding pry.
In the end, I combined the two answers:
# When in the context of binding.pry, __FILE__ resolves to '(pry)',
# binding contains the local variable _file_ which always resolves to
# the current file, even when being evaluated in the context of binding.pry .
# _file_ is only available, in binding. This does the trick:
current_file = __FILE__.downcase == '(pry)' ? _file_ : __FILE__

Rspec any_instance.stub raises undefined method `any_instance_recorder_for' for nil:NilClass exception

Here is the class that I'm testing contained in Foo.rb:
class Foo
def bar
return 2
end
end
Here is the my test contained in Foo_spec.rb:
require "./Foo.rb"
describe "Foo" do
before(:all) do
puts "#{Foo == nil}"
Foo.any_instance.stub(:bar).and_return(1)
end
it "should pass this" do
f = Foo.new
f.bar.should eq 1
end
end
I am getting the following output:
false
F
Failures:
1) Foo Should pass this
Failure/Error: Foo.any_instance.stub(:bar).and_return(1)
NoMethodError:
undefined method `any_instance_recorder_for' for nil:NilClass
# ./Foo_spec.rb:6:in `block (2 levels) in <top (required)>'
Finished in 0 seconds
1 example, 1 failure
Failed examples:
rspec ./Foo_spec.rb:9 # Foo Should pass this
I've consulted the doc and the example given is passing on my machine (so it isn't a problem with the rspec code), but it isn't giving me any information on what I might be doing wrong. The error message is also quite confusing as it's telling me not to call .any_instance on a nil:NilClass, but as I proved with my output, Foo isn't nil. How am I supposed to call .any_instance.stub on my custom object?
I'm using Ruby 1.9.3 and rspec 2.14.5.
You should use before(:each) for stubbing.
Stubs in before(:all) are not supported. The reason is that all stubs and mocks get cleared out after each example, so any stub that is set in before(:all) would work in the first example that happens to run in that group, but not for any others.
rspec-mocks readme
From Rspec 3 any_instance is not defined anymore.
Now use:
allow_any_instance_of(Foo).to receive(:bar).and_return(1)
Source for this and older versions:
https://makandracards.com/makandra/2561-stub-methods-on-any-instance-of-a-class-in-rspec-1-and-rspec-2
Updating rspec worked for me. You can do it using the following command:
bundle update rspec

In RSpec, using let to assign a regex creates unexpected pass/fail behavior - Bug or user error?

File with object under test: foo.rb
class Foo
def a_string
"abcdef8"
end
end
Spec file: foo_spec.rb
require_relative "./foo"
describe Foo do
let(:foo) {Foo.new}
let(:my_matcher) {/^[a-z]+(\d)$/}
# This test passes
it "should match and pass" do
my_str = foo.a_string
my_matcher # <--- why does this affect the test?
matcher = my_str.match(my_matcher)
8.should == matcher[1].to_i
end
# This test fails
it "should also match and pass but fails" do
my_str = foo.a_string
#my_matcher #<---- the only change between the tests
matcher = my_str.match(my_matcher) #<---- when called here, it behaves differently
8.should == matcher[1].to_i
end
end
rspec foo_spec.rb
.F
Failures:
1) Foo should also match and pass but fails
Failure/Error: 8.should == matcher[1].to_i
NoMethodError:
undefined method `[]' for /^[a-z]+(\d)$/:Regexp
# ./foo_spec.rb:18:in `block (2 levels) in <top (required)>'
Finished in 0.00095 seconds
2 examples, 1 failure
Failed examples:
rspec ./foo_spec.rb:14 # Foo should also match and pass but fails
The only difference in the two tests is whether my_matcher is invoked. I first noticed this problem when I was inspecting my_matcher (i.e. p my_matcher), but it also occurs with just invoking my_matcher.
Is this a bug, or am I doing something wrong? Maybe it has something to do with capturing Regex data?
It seems incredibly odd behavior, especially for ruby.
For what its worth, it's an easy (if slightly less DRY) fix. If my_matcher is declared in the it block it works as expected.
That looks like a bug. Can you file an issue with rspec-core?

Confusion with IRB output in Ruby when object#initialize is overloaded

What I actually trying to see when no 'initialize' method is given to an class definition then the class as you said should call the "Object#initialize",which here I tried to customize and see if it has been called or not. With that approach I reached to a conclusion(although that's wrong), when I typed "ob = A .new" that yes I can overload the Object#initialize method.But all has been ended up with the below exception. Then I thought I did something wrong in my customization.So I tried to create the object creation within an exception block and when I typed "begin" and pressed "ENTER" - i got the same error.
>> class A
>> def Object.new initialize
>> p "hi"
>> rescue
>> end
>> end
=> nil
>> begin # <~~~ Here I have pressed on ENTER
"hi" #<~~~~ How was it print out?
/usr/lib/ruby/1.9.1/irb/ruby-token.rb:94:in `Token': undefined method `set_backtrace' for "hi":String (NoMethodError)
from /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:348:in `block in lex_init'
from /usr/lib/ruby/1.9.1/irb/slex.rb:236:in `call'
from /usr/lib/ruby/1.9.1/irb/slex.rb:236:in `match_io'
from /usr/lib/ruby/1.9.1/irb/slex.rb:221:in `match_io'
from /usr/lib/ruby/1.9.1/irb/slex.rb:75:in `match'
from /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:286:in `token'
from /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:262:in `lex'
from /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:233:in `block (2 levels) in each_top_level_statement'
from /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
from /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
from /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
from /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
from /usr/lib/ruby/1.9.1/irb.rb:155:in `eval_input'
from /usr/lib/ruby/1.9.1/irb.rb:70:in `block in start'
from /usr/lib/ruby/1.9.1/irb.rb:69:in `catch'
from /usr/lib/ruby/1.9.1/irb.rb:69:in `start'
from /usr/bin/irb:12:in `<main>'
#ubuntu:~$
Now my questions are -
How has the "hi" been printed?
What is the cause of the error as printed above?
If such initialize definition is not allowed,then why has the error not come after I ended with the class definition?
EDIT
As per #casper I tried below:
>> def Object.new
>> p "hi"
>> end
=> nil
>> begin
/usr/lib/ruby/1.9.1/irb/ruby-token.rb:96: stack level too deep (SystemStackError)
But here no "hi" printed back.
So what made the "hi" to print back in the first case?
What exactly are you trying to do? You just redefined Object.new, so there is no surprise you make everything go haywire.
You can basically get the same effect by just:
>> def Object.new
>> end
>> [press enter]
KABOOM
The reason "hi" is printed is that someone just called Object.new, probably the irb REPL loop, and it expected an object, but instead it gets gobledygook.
You can also try this:
def Object.new *args
p args
end
And you will see funny stuff. However you won't be able to quit irb or do anything useful with it after that. Again: you just broke Object.
To make some sense of it you should read this:
In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?
And then you can try this:
class Object
class << self
alias :old_new :new
end
end
Now you can do:
def Object.new *args
p args
old_new *args
end
This won't break new because you are still calling the old version of it. However you will now be printing out stuff every time someone calls new.

How to run a file in pry that takes arguments

I can start a pry session of a command line app like this
pry -r ./todo.rb
However, if I want to call the list function
pry -r ./todo.rb list
I'm getting an error message.
Without pry, I call the list function
ruby todo.rb list
This is the error message
/Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/gems/pry-0.9.10/lib/pry/repl_file_loader.rb:16:in `initialize': No such file: /Users/michaeljohnmitchell/Sites/todo/bin/list (RuntimeError)
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/gems/pry-0.9.10/lib/pry/pry_class.rb:161:in `new'
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/gems/pry-0.9.10/lib/pry/pry_class.rb:161:in `load_file_through_repl'
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/gems/pry-0.9.10/lib/pry/cli.rb:162:in `block in <top (required)>'
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/gems/pry-0.9.10/lib/pry/cli.rb:65:in `call'
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/gems/pry-0.9.10/lib/pry/cli.rb:65:in `block in parse_options'
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/gems/pry-0.9.10/lib/pry/cli.rb:65:in `each'
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/gems/pry-0.9.10/lib/pry/cli.rb:65:in `parse_options'
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/gems/pry-0.9.10/bin/pry:16:in `<top (required)>'
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/bin/pry:19:in `load'
from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290#global/bin/pry:19:in `<main>'
Source Code
TODO_FILE = 'todo.txt'
def read_todo(line)
line.chomp.split(/,/)
end
def write_todo(file,name,created=Time.now,completed='')
file.puts("#{name},#{created},#{completed}")
end
command = ARGV.shift
case command
when 'new'
new_task = ARGV.shift
File.open(TODO_FILE,'a') do |file|
write_todo(file,new_task)
puts "Task added."
end
when 'list'
File.open(TODO_FILE,'r') do |file|
counter = 1
file.readlines.each do |line|
name,created,completed = read_todo(line)
printf("%3d - %s\n",counter,name)
printf(" Created : %s\n",created)
unless completed.nil?
printf(" Completed : %s\n",completed)
end
counter += 1
end
end
when 'done'
task_number = ARGV.shift.to_i
binding.pry
File.open(TODO_FILE,'r') do |file|
File.open("#{TODO_FILE}.new",'w') do |new_file|
counter = 1
file.readlines.each do |line|
name,created,completed = read_todo(line)
if task_number == counter
write_todo(new_file,name,created,Time.now)
puts "Task #{counter} completed"
else
write_todo(new_file,name,created,completed)
end
counter += 1
end
end
end
`mv #{TODO_FILE}.new #{TODO_FILE}`
end
Update
when I try
pry -r ./todo.rb -e list
I'm getting the following error
NameError: undefined local variable or method `list' for main:Object
From pry --help:
-e, --exec A line of code to execute in context before the session starts
So, if your list method is defined on main (if you don't know, it probably is), then you can do this:
pry -r ./todo.rb -e list
Update
Pry doesn't let you pass in arguments for scripts it loads (or at least it isn't documented). But all is not lost, you can call pry from your script. Just drop this at wherever you want to inspect:
require 'pry'; binding.pry
This will spawn a pry session that has access to all the local variables and methods.
I think you can use:
ruby -rpry ./todo.rb -e list

Resources