In rails automated testing how do I spawn the console? - ruby-on-rails

I'm writing some automated tests for an app using rails 3.1. One of my tests is failing and at the point of failure I want to be able to jump into the rails console and inspect the state of things. Leading up to this assertion a lot of objects were created and states were changed.
After the test suite finishes running the test database is cleared.
I remember there was some way to insert a breakpoint in the test file or something similar which would throw me into the console. I've done a ton of googling and can't find it.
Anyone?

Require ruby-debug on your Gemfile.
If on 1.8:
gem 'ruby-debug'
If on 1.9:
gem 'ruby-debug19', :require => 'ruby-debug'
Note that if you're on 1.9.3 requires a few tweaks.
Finally, put a debugger statement where you want to debug, and run your tests. It should popup a console.
I'd also recommend taking a look at pry, as Amadan stated.

Use Pry, and say binding.pry as a breakpoint.
You might like this Railscast.

Related

Debugging in rails 3

i want to debug ROR without going through the effort of putting inspect method for every relevant object in the controller as well in the model.is there a better way as we have in Java (Run time debugger using eclipse).i know that I can Use Rails.logger and also make use of rails Console(irb`).i am even aware of debugging/inspecting elements in erb/rb file.Still is there a better,quick and reliable way to debug a Rails app.
There is much better, see this railscats.
It presents two great gems, especially Better Errors
Otherwise, you could use pry with rails, see this railscast.
you can also use pry-rails, pry-debugger and then use binding.pry method in your code and then while using your app you have Rails console available in rails server
Add this lines to your application's Gemfile
group :development do
gem 'ruby-debug19'
end
then run cammand
bundle install
add debugger within your controller or model method, stop the rails server and restart again. Whenever rails found word debugger it stops control at that point. You can easily debug your value or object.
Hope this will helps you.

How to disable some gems during the testing process?

I have a gem installed on my rails 3 application that runs smoothly, But when I run the cucumber tests, that gem is missing to get the user atributes. And actually it is the way it should be because of the security management. ( no login == no information )
So, I am wondering if there is any way to disable this gem while I am running the cucumber test ? so that i get no error based on this gem.
thanks in advance !
In your gemfile, you can add the gem in a particular environment. i.e.
group :development, :production do
gem 'yahoo-weather'
end
This gem will only be added in the development and production env only. It wont be present in the testing environment.
After checking the problem with my colleague, we decided on the solution that seems almost OK to do it in this way,
Since we dont want the specific gem function to get into play during the test process, we decided to use
acts_as_specificgem(do this do that) unless Rails.env == "test"
No need to repeat ourselves at test scenarios and looks more clean.
cheers!

Ruby / Rails debugging strategy

Please can you share your approach / methodology to debugging in Ruby / Rails.
I'm busy with the Rails tutorial, and am getting the error:
NoMethodError in UsersController#show
undefined method `microposts' for #<User:0x83b43e8>
And that got me thinking about debugging strategies. Does anyone have advice for a new Rails user (and new MVC user) on strategies to approach debugging. What path do you follow? Is there a generally accepted approach? Is there a way to step through the code?
Right now I am using unit testing as a kind of "lint" checker, but that only goes so far.
Although I want to solve it, the actual error I am getting right now is not the main thrust of this question.
(PS: The problem is not a duplicated "show" as documented in elsewhere on Stackoverflow
I haven't seen this mentioned yet but another option is to put a debugger statement in your code. Like this
def some_method
something = 3
debugger
# ... more code
end
If this is in a rails app when the code reaches debugger it will cause the terminal window running the web server to jump into something that looks like an irb session (I'm not exactly sure what it is). From there you can do puts something to see what the value is for example. You can even puts params to see what all the params values are. You can also step through the code, jump to a specific line, etc.
Pry seems to be a better way to go about this but it's how I used to debug before I knew about pry.
Note: You might need to do require 'ruby-debug' if you're doing this outside of rails.
i use a combination of irb, print statements, logging and pry bindings. (pry is a great gem)
irb is a great way to just play around with your ruby or rails app in the console. You could just enter the code from your controller (or similar) and see if it breaks in console for faster feedback loop. But remember you have to do reload! if you change anything in your class/module.
print statements are easy if you're running tests and just want it to output something a different points in your test. But if your testing in a browser I would recommend writing to the logger: Rails.logger.debug "...". But remember to set your logging level in your configuration to DEBUG -or- you can just do Rails.logger.info instead which should show up by default. Then you can just tail or view the logs in my_app/logs/development.rb.
My favorite method for really tricky bugs is that if the error is happening in a test you can just place binding.pry in the preceding line and then it will pause your test at that line and drop you into a console. I recommend watching the rails casts for more in-depth info: http://railscasts.com/episodes/280-pry-with-rails
I do not start Rails project without 'pry' gem.
Add gem to Genfile:
group :development, :test do
gem 'pry'
end
and stop request execution anywhere in your project, just put
binding.pry
to your model, controller, tests ..., or
<% binding.pry %>
in your view's, templates, partials.
Then you can check what ever you want objects, params, variables ...
Type exit to leave pry environment, and request will continue.
The Ruby on Rails Guide would be a great place to start, but there's plenty more.
I always have a rails console session or at minimum an irb session to play with to see if things do what I think they do.
I also use RubyMine which has an excellent integrated debugger http://www.jetbrains.com/ruby/
Beside pry gem, another option would be byebug. This gem enables you to temporarily stop code execution at a breakpoint, which is marked with keyword byebug inside the code. When execution reaches the breakpoint, a marker will be pointing to the current line, and you are able to type in commands.
This Gem offers a huge set of commands, the most commonly used ones are:
next - this command enables you to go to next line
step - goes into each invoked method step by step
break - it stops the execution of code
continue - continues code execution
This is a great article to check for debugging in rails.

How do I ruby-debug in a spec or feature if I am using Guard and Spork?

I managed to get Cucumber, RSpec, Guard, and Spork play nicely with each other in a Rails 3 app, but now I am stuck with the above problem.
Currently I require 'spork/ext/ruby-debug' in the prefork block of spec_helper.rb but this works only if I run spork manually, without guard.
This worked for me: Replace your 'guard-spork' gem with this one:
gem 'guard-spork', :git => 'https://github.com/irohiroki/guard-spork.git'
More info on this blog post (in Japanese, but Google translate does a decent job).
The author also created this handy Rails 3 example app to show how to set things up.
Adding Guard to Spork makes it much more useful IMO. I may have to go back to using it now :)
If you still have problems (or even if you don't,) I would recommend using the pry and pry-nav gems instead of Ruby debug.. they work flawlessly with guard/spork/rspec/cucumber and test unit.

How do you debug a Sinatra app like a Rails app?

In my main Sinatra controller, I want to debug the params hash after it is POSTed from a form.
I have added:
puts params.inspect
and
set :logging, :true
The params.inspect works if everything goes well. But if an error happens before the controller is executed I'm not getting any information about the error like I would in Rails by default.
What's the best way to get useful debug info?
This example did not work at all (the app wouldn't even start after I added this code):
configure do
Log = Logger.new("sinatra.log")
Log.level = Logger::INFO
end
followed by:
Log.info "#{#users.inspect}"
You could try adding a before filter that prints out the parameters
before do
puts '[Params]'
p params
end
My opinion is that for debug you should use configure :development do because some debugging flags are turned on in this scenario. So, under your configure do block you can enable the flags:
enable :logging, :dump_errors, :raise_errors
and for your logging facility:
log = File.new("sinatra.log", "a")
STDOUT.reopen(log)
STDERR.reopen(log)
From the Sinatra handbook:
dump_errors option controls whether the backtrace is dumped to rack.errors when an exception is raised from a route. The option is enabled by default for top-level apps.
raise_errors - allow exceptions to propagate outside of the app
(...)
The :raise_errors option is disabled by default for classic style apps and enabled by default for Sinatra::Base subclasses.
I also use the
puts "something" + myvar.inspect
method in the middle of my controllers.
As #jshen says, ruby-debug is a very nice tool - if you are using Ruby 1.8.
In order to use it in Sinatra, insert
require 'ruby-debug/debugger'
where you'd like debugging to start.
I recommend using Pry or the Ruby debugger. With Pry, you can use the command line to traverse your methods and variables like you would in bash.
See "How to use Pry with Sinatra?".
In order to find the best way to debug sinatra app, I created a repo at github. The most useful part is step into method debug, which looks like below.
Here is the repo: https://github.com/hlee/sinatra_debugger_example
I'd highly recommend using ruby-debug for Ruby 1.8. It's very easy to setup and use once you learn the four or five basic commands. It will allow you to set a breakpoint where you want to inspect the params and interactively play with it like you would in IRB.
Ruby Debuggers in Sinatra
You are free to use Ruby debuggers for Sinatra. The main issue is that you will need to know which Ruby Gem is used for which Ruby version. For most people now that means Ruby 2.X and byebug.
Ruby Debuggers by Ruby version
Ruby Version Gem Install require add breakpoint
---------------------------------------------------------------------
1.8.X ruby-debug 'ruby-debug/debugger' debugger
1.9.X debugger 'debugger' debugger
2.0.0+ byebug 'byebug' byebug
Once you know that you must install the gem, require it in your code and then add breakpoints by typing a keyword. Taking byebug as an example:
install: gem install byebug
require: require 'byebug'
breakpoint: byebug
Further Reading
Byebug project
Byebug guide to use a terminal debugger
Have you thought about trying something like this article: http://www.gittr.com/index.php/archive/logging-with-sinatra-and-passenger-another-try/
I'm guessing since you mentioned your main Sinatra controller, you have more than one, which means you're subclassing Sinatra::Base rather than using a classic (top-level) Sinatra app. If this is indeed the case, a lot of the default error-handling that Sinatra does is disabled by default.
If you include set :show_exceptions, true if development? in the class definition, you'll get friendly error pages that include a stack trace, params, etc, instead of just an internal server error. Another option is to set :raise_errors, false and then include a error do ... end block that will run whenever one of your routes raises an error.
I'd recommend using the debugger gem for Ruby 1.9.
To use this with your Sinatra app:
Add the gem to your Gemfile
gem "debugger"
Install the gem by running
bundle
In your main sinatra app file, add
require 'debugger'
Now to debug, you just have to add the following line
debugger
anywhere in your code.
When the code is being executed and debugger is seen, code would stop and you can inspect variables, run commands (using eval), etc.

Resources