I'm trying to get Ruby debugger running in one of my specs:
describe User do
it "should be valid" do
debugger
User.new.should be_valid
end
end
When I run rspec though, I get:
debugger statement ignored, use -d or --debug option to enable debugging
I've tried the following:
rake spec --debug
rake spec --debug --trace
rake spec:models --debug
bundle exec rspec --debug
bundle exec rspec --debug spec/models/
bundle exec rspec --d spec/models/
bundle exec "rspec --debug" spec/models/
bundle exec rspec --debugger spec/models/
bundle exec --debugger rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle exec --debugger rspec spec/models/
bundle exec rspec --debugger spec/models/
Any ideas on how to exec rspec in the right way? I'm on Rails 3.0.5, Ruby 1.9.2, RSpec 2.5.1, ruby-debug19.
Thanks,
Justin.
You will get what you want by including require 'ruby-debug' at the top of your spec:
# spec/models/user_spec.rb
require 'spec_helper'
require 'ruby-debug'
describe User do
it "should be valid" do
debugger
User.new.should be_valid
end
end
You would then run rake spec or rspec as normal
NOTE: I now prefer Ruby 2.0+ and pry. It is pretty much the same process:
require 'spec_helper'
require 'pry-debugger'
describe User do
it "should be valid" do
binding.pry
expect(User.new).to be_valid
end
end
Also, I generally put requires like this in my spec_helper file, so that pry-debugger is available to all of my specs.
You can create an .rspec configuration file in the root of your project and include the line:
--debug
For Ruby >= 1.9.2
You should install the debugger gem instead of ruby-debug19. It you use bundler, you just put this in your Gemfile:
group :test do
gem "debugger"
end
After that you can just put
rspec < 3.0
--debug
rspec >= 3.0
-rdebugger
in your .rspec file
Then you can just run
bundle exec rake spec
without any additional arguments. There is no need to modify your source code either (not even your test source code)
For ruby 2.0 I use byebug: https://github.com/deivid-rodriguez/byebug
gem 'byebug'
Code:
# spec/models/user_spec.rb
require 'spec_helper'
require 'byebug'
describe User do
it "should be valid" do
byebug
User.new.should be_valid
end
end
The best way I have found to debug in rSpec is by adding the following to your 'spec_helper.rb' file
def logger
Rails.logger
end
You can then access all the logger methods within your rSpec files and incorporate such things as tagged logging. This of course is for Rails 3 and up. If you have anything prior to Rails 3 then add this instead:
def logger
RAILS_DEFAULT_LOGGER
end
Once you have your logging statements in place you can enter
tail -f log/test.log
in your terminal shell in order to watch your logging statements while the tests are run.
Of course in your actual rspec test you would enter something such as
logger.debug "#{1.class}" # => Fixnum
If you want to filter your debug statements from the rest of your test log simply prepend a random string on to your debug statement and pipe the output of the tail command to grep.
Example:
logger.debug "random_string #{1.class}" # => Fixnum
tail -f log/test.log | grep random_string
Update
I've changed my opinion on this. You should install pry, pry-doc, and pry-debug, pry-debugger, and pry-rails. Then use binding.pry in your code to open an interactive debugger console that rules the world!
The best and cleanest option is to use --require in your .rspec file. What you put depends on which gem you use for debugging.
--color
--require pry
--require rails_helper
These correspond to command line options (-d or --debug is now deprecated).
Feel free to use debugger, ruby-debug or pry (pry-rails in your Gemfile).
For your Gemfile:
group :test, :development do
gem 'pry-rails'
end
Putting require 'ruby-debug' etc. at the top of your spec is simply more tightly coupled -- especially since here the top voted comment suggests putting it individually in ALL your files. With the new .rspec file you shouldn't need to put require 'spec_helper' or require 'rails_helper' at the top of your files anymore.
They make more sense as implicit command line arguments.
Related
Surprisingly, I didn't find the standard way to make the output of Rails Minitest colorful. There're some workarounds, though.
So what's the way to do that?
Add minitest-rg to the test group in your Gemfile:
group :test do
gem "minitest-rg"
end
Then require minitest/rg in your test/test_helper.rb:
require "minitest/rg"
Now you have colorful test output when running rake test.
Sure, in this rakefile change it to:
namespace :test do
task :isolated do
Dir.glob("test/**/*_test.rb").all? do |file|
sh(Gem.ruby, '-w', '-Ilib:test', file, '-p')
end or raise "Failures"
end
end
Then from the console run rake test:isolated while in activesupport folder and watch the rainbow go! This is using minitests built in pride library for color.
I'm attempting to run some tests on my rails application, and they're working, which is great. However, I'm noticing that when I just run rake it defaults to running my tests. If anybody has run into this before and can shed some light on why this is happening, I'd appreciate it.
I'm using
rails 4.1.0
ruby 2.0.0
factory girl rails
minitest rails
minitest rails capybara
database cleaner
Rakefile
require File.expand_path('../config/application', __FILE__)
Pinteresting::Application.load_tasks
namespace :test do
task :run do
ENV["RACK_ENV"] = "test"
$LOAD_PATH.unshift("lib", "spec")
if ARGV[1]
require_relative ARGV[1]
else
Dir.glob("./spec/**/*_spec.rb").each { |file| require file }
end
end
end
The default rake task is defined in rails/railties/Rakefile, and it runs all unit tests by default.
I'm trying to learn RoR following this tutorial and I'm currently in chapter 3. The tutorial works fine if I follow it line-by-line. However, the commands used in the tutorial suppress generation of default tests. When I try to keep them and possibly use them in my project, I always hit a wall somewhere.
Could you please tell me what I'm doing wrong?
$ rails new myproject
$ cd myproject/
$ echo "gem 'rspec'" >> Gemfile
$ echo "gem 'rspec-rails'" >> Gemfile
$ echo "gem 'capybara'" >> Gemfile
$ bundle install
$ bundle --binstubs
$ rails generate rspec:install
$ rails generate controller StaticPages home help about
Then I edit the spec/views/static_pages/home.html.erb_spec.rb file, to test whether capybara works:
require 'spec_helper'
#require 'capybara'
#require 'capybara/rails'
#require 'capybara/rspec'
describe "static_pages/home.html.erb" do
it 'should have a right title' do
visit '/static_pages/home'
page.should have_selector('title', :text => 'Home')
end
end
Running bin/rspec at this point, obviously, ends up with a failure. Well, a failure could have been expected. The reason for one of these failures is more alarming, though:
1) static_pages/home.html.erb should have a right title
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_5:0x00000003dfd268>
# ./spec/views/static_pages/home.html.erb_spec.rb:7:in `block (2 levels) in <top (required)>'
The visit method, which AFAIK is part of Capybara, has not been found. Uncommenting the three extra requires in home.html.erb_spec.rb does not change anything in the result.
Any ideas what I'm doing wrong? Or what I should do better?
Rails version: 3.2.6
Put your test in requests directory instead of views.
I'm using Rails3 (Windows, Ruby 1.8.7) with rufus-scheduler gem. Gem works fine, but if I'm trying to run some standard rake task, error occurs:
Don't know how to build task 'db:version' # ofc, db:version is just example
Terminal command
rake -T
works
If I'm trying to define own simple rake commands, they works fine too:
# /lib/my_scheduler.rb
require 'rubygems'
require 'rake'
require 'rufus/scheduler'
load File.join( Rails.root, 'lib', 'tasks', 'my_own_tasks.rake')
scheduler = Rufus::Scheduler.start_new
scheduler.every '5s' do
Rake::Task["my_own_namespace:test"].invoke
end
end
# /lib/tasks/my_own_tasks.rb
namespace :my_own_namespace do
task :test do
puts "Some scheduler task"
end
end
... but using standard rake tasks *in my_own_tasks* throws the same error.
Some help would be appreciated
PS. I'm newbie, so sorry, if that was dumb question
Maybe someone will need solution:
system("rake namespace:task")
f.e:
system("rake db:version")
I'm learning how to do integration testin on my rails app. I started with cucumber but then learned since I'm just testing a models method that I should be using RSPEC. So now I'm trying to get RSPEC installed.
When I run autotest, it is only looking at the /features directory not the /spec directory.
Here's what I've done so far:
.autotest
require 'autotest/growl'
require 'autotest/fsevent'
Autotest.add_hook(:initialize) {|at|
at.add_exception %r{^\.git} # ignore Version Control System
at.add_exception %r{^./tmp} # ignore temp files, lest autotest will run again, and again...
# at.clear_mappings # take out the default (test/test*rb)
at.add_mapping(%r{^lib/.*\.rb$}) {|f, _|
Dir['spec/**/*.rb']
}
nil
}
/spec - created directory
/spec/spec_helper.rb - created
/spec/lib/mailingjob_spec.rb - created, tried to write a case that would fail as follows:
require 'spec_helper'
describe User do
before(:each) do
#valid_attributes = {
:login => "akm",
:email => "akm2000#gmail.com",
:password => "D1ff1cultPa55w0rd",
:password_confirmation => "D1ff1cultPa55w0rd"
}
end
it "should create a new instance given valid attributes" do
User.create!(#valid_attributesXXXX)
end
end
But when I run autotest this rpsec is never run only the features dir with cucumber is running. Ideas? thanks
Try:
rspec --configure autotest
From the autotest README:
[RSpec] if you want to use require
'spec_helper' -> rspec --configure
autotest