pry session during rspec test ends on any input - ruby

I am trying to pause an rspec test using binding.pry to understand why it fails. Here is a simplified version:
def copy_db
binding.pry
puts 'hello world'
end
The binding.pry pauses execution but any input in the session causes it to end. Any ideas why this would be? This is not a rails project.

I'm not sure if the previous answer was sufficient, but another thing you could try besides binding.pry is using the byebug gem.
In your Gemfile add:
gem "byebug"
In the terminal (at the root of the project directory) run:
$ bundle install
Require 'byebug' at the top of your code and insert a 'debugger' wherever you want to set a breakpoint:
require 'byebug'
def copy_db
debugger
puts 'hello world'
end
And it will pause rspec and enter the byebug debugger, which lets you display the values of various variables as you step through each line.
[1, 4] in path/to/your/file.rb
1: def copy_db
2: debugger
=> 3: puts 'hello world'
4: end
(byebug)_

Related

Byebug not responding to commands?

I have been having an issue lately. When trying to debug anything I run my file and use Byebug. However when I run any command on Byebug my main menu command line of terminal appears. Ex:
*****$: ruby file.rb
require "byebug"
debugger
1: def array(array_1, array_2)
if array_1 == array_2
true
else
false
end
end
(byebug) c
*****$:

running rspec with "-e" parameter

I can run the simple ruby program like
ruby -e "puts 'raja'"
but when I run Rspec file, I run this way
ruby rspec my_example_spec.rb
Said that, Do I have any way to pass the rspec program as parameter as I have done in the first line?
I tried
ruby -e rspec "require 'rspec'
require 'watir'
describe 'My behaviour' do
it 'should do something' do
b = Watir::Browser.new
b.goto 'www.google.com'
b.text_field(name: 'q').set 'Rajagopalan'
sleep 2
b.close
end
end"
But it's not running. How can I pass rspec program with '-e' parameter?
Googling a bit, I found this issue thread on the RSpec github: https://github.com/rspec/rspec-core/issues/359
It seems you can run your RSpec tests from Ruby by putting
RSpec::Core::Runner::run({}, $stderr, $stdout)
after your definitions.
The following minimal test works for me:
ruby -e "require 'rspec'
describe 'My behaviour' do
it 'should do something' do
expect(1).to eq(2)
end
end
RSpec::Core::Runner::run({}, \$stderr, \$stdout)
"

Is there any way to run Rspec tests on a single Ruby file?

I would like to run some tests on some code kata exercises that I am working on. Is it possible to run Rspec tests on a single Ruby file? I tried adding require 'rspec' to the top of the file and then the command rsepc from the project dir but the following is returned:
F
Failures:
1) Sentence reverser reverses the words in a sentence
Failure/Error: expect(sentence_reverser(test_sentence)).to eq('I am backwards')
NoMethodError:
undefined method `sentence_reverser' for #<RSpec::ExampleGroups::SentenceReverser:0x0000559764dc5950>
The code I have is:
require 'rspec'
def sentence_reverser str
str.split.reverse.join(' ')
end
describe "Sentence reverser" do
it 'reverses the words in a sentence' do
test_sentence = "backwards am I"
expect(sentence_reverser(test_sentence)).to eq('I am backwards')
end
end
Try running rspec </path/to/kata.rb>. You shouldn't even require 'rspec' then - just tested your example.

Could not find 'rspec/autorun'

I'm trying to do the first example of The rspec book
greeter_spec.rb
class RSpecGreeter
def greet
"Hello RSpec!"
end
end
describe "RSpec Greeter" do
it "should say 'Hello RSpec!' when it receives the greet() message" do
greeter = RSpecGreeter.new
greeting = greeter.greet
greeting.should == "Hello RSpec!"
end
end
When I run $ rspec greeter_spec.rb the output should be something like this:
.
Finished in 0.00075 seconds
1 example, 0 failures
but I got:
Could not find 'rspec/autorun'
This may happen if you're using rubygems as your package manager, but it is not
being required through some mechanism before executing the rspec command.
You may need to do one of the following in your shell:
# for bash/zsh
export RUBYOPT=rubygems
# for csh, etc.
set RUBYOPT=rubygems
For background, please see http://gist.github.com/54177.
I tried to include require 'rspec/autorun' at the top of the file but doesn't work, also I did what they suggest on the output but still not working.
I'm using ruby version 2.0.0p648, and rspec 2.0.0

How do I drop to the IRB prompt from a running script?

Can I drop to an IRB prompt from a running Ruby script?
I want to run a script, but then have it give me an IRB prompt at a point in the program with the current state of the program, but not just by running rdebug and having a breakpoint.
Pry (an IRB alternative) also lets you do this, in fact it was designed from the ground up for exactly this use case :)
It's as easy as putting binding.pry at the point you want to start the session:
require 'pry'
x = 10
binding.pry
And inside the session:
pry(main)> puts x
=> 10
Check out the website: http://pry.github.com
Pry let's you:
drop into a session at any point in your code
view method source code
view method documentation (not using RI so you dont have to pre-generate it)
pop in and out of different contexts
syntax highlighting
gist integration
view and replay history
open editors to edit methods using edit obj.my_method syntax
A tonne more great and original features
you can use ruby-debug to get access to irb
require 'rubygems'
require 'ruby-debug'
x = 23
puts "welcome"
debugger
puts "end"
when program reaches debugger you will get access to irb.
apparently it requires a chunk of code to drop into irb.
Here's the link (seems to work well).
http://jameskilton.com/2009/04/02/embedding-irb-into-your-ruby-application
require 'irb'
module IRB
def self.start_session(binding) # call this method to drop into irb
unless #__initialized
args = ARGV
ARGV.replace(ARGV.dup)
IRB.setup(nil)
ARGV.replace(args)
#__initialized = true
end
workspace = WorkSpace.new(binding)
irb = Irb.new(workspace)
#CONF[:IRB_RC].call(irb.context) if #CONF[:IRB_RC]
#CONF[:MAIN_CONTEXT] = irb.context
catch(:IRB_EXIT) do
irb.eval_input
end
end
end
This feature is available from Ruby 2.4. You can just use binding.irb
E.g.
require 'irb'
a = 10
binding.irb
puts a
If you run above code, you will get irb console, so that you can inspect values of local variables and anything else that is in scope.
Source: http://blog.redpanthers.co/new-binding-irb-introduced-ruby-2-4/
Ruby commit: https://github.com/ruby/ruby/commit/493e48897421d176a8faf0f0820323d79ecdf94a
Just add this line to where you want the breakpoint:
require 'ruby-debug';debugger
but i suggest use pry instead of irb, which is super handy, insert the following line instead:
require 'pry'; binding.pry
I'm quite late to the game but if you're loading a script from within irb/pry already, a simple raise also works to pop you back out to the irb/pry prompt. I use this quite often when writing one off scripts within the rails console.

Resources