This is a short question: I am looking for a way to run specs in debug mode, with the -u switch, so that RSpec would drop to console whenever it failed, without having to add a debugger line into the code. Any pointers?
Will answer my own question.
Following this tutorial, I created a custom formatter, as in:
require "spec/runner/formatter/specdoc_formatter"
class DebuggerFormatter < Spec::Runner::Formatter::SpecdocFormatter
def example_failed(example, counter, failure)
super
debugger if Kernel.respond_to?(:debugger)
end
end
hakanensari, your code seems to break inside rspec. It'd be nice if we could break at the failing assert line.
Related
I have the following script in a file called foo.rb
def foo(msg)
msg
end
def bar
thing = 123
thing
end
debugger
p foo(:hai)
I run the program in debug mode, like so:
ruby --debug -r debug foo.rb
Notice I make sure the stdlib debug is loaded via -r and I also put the ruby environment into debug mode using --debug
So the first output I get is unexpected:
Debug.rb
Emacs support available.
/Users/M/.rbenv/versions/2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:57: RUBYGEMS_ACTIVATION_MONITOR.enter
After that, if I press c to 'continue' the program ends with the following error:
Exception `NameError' at foo.rb:10 - undefined local variable or method `debugger' for main:Object foo.rb:10:in `<main>': undefined local variable or method `debugger' for main:Object (NameError)
Can anyone tell me what I'm doing wrong, and how to actually get debug mode to recognise the relevant debugger command (if that's even the correct command to be using; the docs aren't clear at all on this)
Note: I'm not interested in using 3rd party gems in this instance (e.g. pry or ruby-debug). I'm only interested in understanding how to use the stdlib debugger that comes with Ruby
Update
Since this question was answered, I've gone ahead and created a gist for future reference. For anyone stumbling across this thread you might find it useful: https://gist.github.com/Integralist/5658cb218bb50494a1fa
Don't use -r. The entire mechanism by which an interactive debugging sessions is loaded is by the literal line of code require 'debug'.
Your code should look like this:
def foo(msg)
msg
end
def bar
thing = 123
thing
end
require 'debug'
p foo(:hai)
And you should run the program by simply typing ruby <program_name>.rb.
I want to make it clear this problem is not about backticks. I am new to Cucumber and am trying to run the code from second chapter of The Cucumber Book: Behavior-Driven Development for Testers and Developers. I am using Cucumber 1.3.19 with ruby 1.9.3p551 in Windows 7 with Ansicon x64 1.60. Cucumber works fine with other code I have received from others, so the configuration is good. I have read several posts about problems with this tutorial in regards to the backticks already; however, I have copied the code directly from the Cucumber Book website with the correct backticks (not single quotes) and am still getting the error.
Command failed! <RuntimeError>
./features/step_definitions/calculator_steps.rb:15 in '/^the calculator is run$/
features\adding.features:5:in 'When the calculator is run'
Failing Scenarios:
cucumber features\adding.feature: 3
My code for adding.feature looks like
Feature: Adding
Scenario: Add two numbers
Given the input "2+2"
When the calculator is run
Then the output should be "4"
My code for the features/step_definitions/calculator_steps.rb looks like:
Given /^the input "([^"]*)"$/ do |input|
#input = input
end
When /^the calculator is run$/ do
#output = `ruby calc.rb #{#input}`
raise('Command failed!') unless $?.success?
end
Then /^the output should be "([^"]*)"$/ do |expected_output|
#output.should == expected_output
end
Is there something with the newer version of Cucumber that could render this code bad?
Thanks for the help. I figured the problem out. calc.rb needed to be in the root directory which was above features and I was putting it in the step_definitions :P.
I'm working on an application that's a glorified wrapper around irb, and I would like to override the "help" command (which by default seems to be an alias to irb_help). I'm using the approach of executing the following code before allowing the user to do anything in the prompt:
class << self
def help
my_custom_help
end
end
While this seems to work, I get an annoying warn message every time I start the application:
irb: warn: can't alias help from irb_help.
Is there any way to suppress this warning message?
I have some test on Rspec, which looks like this:
describe "description" do
before :each do
do_before()
end
it "something_1" do
...
end
it "something_2" do
...
end
end
I know that I can get name of current test ("something_1") by using
example.description
Is there any way to get array of all descriptions in before :each area?
rspec -f d --color --dry-run filename
Works for me in rspec 3.5.2, lists all tests without running them
There used to be a way to do this using the tag --dry-run, but that has been removed and no longer works.
You can use the -fd which is for format = documentation. This will show you a long list of all the specs that you've done and what they look like. It does, however, still run the test and show you any errors you have in the process. That said it's still a great way to list of all of your tests.
Right now if I run my test suite using rake spec I get an error:
1) SegmentsController GET 'index' should work
Failure/Error: get 'index'
undefined method `locale' for #
# ./spec/controllers/segments_controller_spec.rb:14:
in `block (3 levels) in '
This is normal as I do have an error :)
The problem is that the trace isn't very helpful. I know it broke in segments_controller_spec.rb, line 14, but this is just where I call the test:
### segments_controller_spec.rb:14
get 'index'
I would prefer to have the actual line breaking and the complete trace, not the part in the spec folder.
Running with --trace doesn't help.
You must run rspec with -b option to see full backtraces
Another (easier) alternative is to edit the .rspec file, and add the backtrace option.
It should look somewhat like this:
--colour
--backtrace
That will give you the full backtrace.
Hope this helps.
This will also work:
# rails_helper.rb
RSpec.configure do |config|
config.full_backtrace = true
end
Another approach is to clear all backtrace exclusion patterns in spec_helper.rb. I like this solution most as I'm able to keep all RSpec settings in one place and get rid of .rspec file or explicit --backtrace in .travis.yml.
# spec_helper.rb
RSpec.configure do |config|
config.backtrace_exclusion_patterns = []
end
I don't know how to get the controller error to show up in rspec. Sometimes it shows up but I don't know what conditions cause it to show up. Here is a way to see the error fairly quickly though:
Open another terminal session and run:
tail -f log/test.log
Then go back to the terminal session and run just the spec that had the error:
bin/rspec -b spec/requests/posts/index_spec.rb
Go back to the tail of the log and you should see the error, hopefully without too much other stuff surrounding it (because you ran the failing test by itself).
One more option when all else fails is to just add a rescue block and print out the stack try or add a binding pry statement there and use show-stack.
rescue Exception => e
puts ""
puts e.backtrace
puts ""