Ruby Debug "no such file to load --spec_helper" - ruby-on-rails

Noob who may be missing something obvious ...
I'm trying to debug an Rspec file. The Rspec file is stripped down at this point:
require 'spec_helper'
describe PagesController do
render_views
describe "GET 'home'" do
describe "when not signed in" do
before(:each) do
get :home
end
it "should be successful" do
response.should be_success
end
it "should have a vendor section" do
response.should have_selector("h1", :content => "Vendor")
end
it "should have a hospital section" do
response.should have_selector("h1", :content => "Hospital")
end
end
end
I make the following call from the command line:
rdebug spec/controllers/pages_controller_spec.rb
The debugger runs, but throws the following error:
> require 'spec_helper'
<internal:lib/rubygems/custom_require>:29:in `require'
<internal:lib/rubygems/custom_require>:29:in `require'
/home/kevin/.rvm/bin/rails_projects/evaluationrx/spec/controllers/pages_controller_spec.rb:1:in `<top (required)>'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/ruby-debug19-0.11.6/bin/rdebug:125:in `debug_load'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/ruby-debug19-0.11.6/bin/rdebug:125:in `debug_program'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/ruby-debug19-0.11.6/bin/rdebug:412:in `<top (required)>'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/bin/rdebug:19:in `load'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/bin/rdebug:19:in `<main>'
Uncaught exception: no such file to load -- spec_helper
Rspec without the debugger without a problem. I'm using Rspec 2.3.0, ruby-debug19 (0.11.6), Rails 3.0.3 and ruby 1.9.2. Why can't the debugger see the spec_helper file?

Make sure you've run
rails generate rspec:install
to create the spec_helper.rb file.

I assume your spec_helper.rb resides in the spec directory? Try:
require_relative '../spec_helper'

Related

Rspec - Syntax errors while testing users controler

I'm having an issue while testing user authentication with rspec and devise.
here is my code:
require 'rails_helper'
describe UsersController, :type => :controller do
let(:user) { User.create!(email: '1#test.com', password: '123456') }
describe 'GET #show' do
context 'User is logged in' do
before
sign_in user
get :show
end
it "loads correct user details" do
expect(assigns(:user)).to eq user
end
end
context 'No user is logged in' do
it 'redirects to login' do
get :show, id: user.id
expect(response).to redirect_to(root_path)
end
end
end
end
When i run rspec on the terminal i get this error:
/Users/joao1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load': /Users/joao1/Desktop/nameofapp/spec/controllers/users_controller_spec.rb:29: syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError)
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/exe/rspec:4:in `<top (required)>'
from /Users/joao1/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `load'
from /Users/joao1/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `<main>'
from /Users/joao1/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/joao1/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
How can i solve this and effectively test my user authentication?
The error message is a result of a typo in your program and it not matching up do end blocks. Looking more carefully I see you are missing a do statement from the before block. Change it to:
before do
sign_in user
get :show
end

Can't Execute $ time rspec spec/requests/static_pages_spec.rb

I'm following the Ruby on Rails Tutorial by Example by Michael Hartl but I got stuck.
Everytime I try to run the following test I got an error:
time rspec spec/requests/static_pages_spec.rb
My error:
/home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load': cannot load such file -- /home/i/rails_projects/sample_app/spec/spec/requests/static_pages_spec.rb (LoadError)
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/rspec:23:in `load'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/rspec:23:in `<main>'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'
real 0m0.752s
user 0m0.699s
sys 0m0.044s
static_pages_spec.rb :
require 'spec_helper'
describe "StaticPages" do
describe "Home page" do
it "It should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the right title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help page" do
it "It should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
it "should have the right title" do
visit '/static_pages/help'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "It should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
end
it "should have the right title" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
end
describe "Contact" do
it "It should have the content 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_content('Contact')
end
end
it "should have the right title" do
visit '/static_pages/contact'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
Gemfile: ## http://pastebin.com/1RhbaCiK
OS: Ubuntu 14.04
I've been searching for solution, but have not found one. Please keep in mind that I'm new to Ruby on Rails and web development.
Your error is saying:
cannot load such file -- /home/i/rails_projects/sample_app/spec/spec/requests/static_pages_spec.rb (LoadError)
which means RSpec can't find the spec. I see you have spec twice in the file path: ../sample_app/spec/spec/...
You should keep your specs under the spec directory of your Rails app. So, the path should be: /sample_app/spec/ like this. Fix that and try again. It should work.
Update
So, after running your code locally, I found the issue. There was a gem dependency issue with gem guard-spork.
You just need to upgrade this gem. In your Gemfile:
Change gem 'guard-spork', '1.5.0' to gem 'guard-spork', '2.1.0'
Then, bundle install
And, then run time rspec
Everything will run smoothly :-)

I have install rspec-rails gem ruby and rail when i run this error comming

I have install rspec-rails like this and create a file static_pages_spec.rb
gem list
272 rails g rspec:install
274 rails generate integrateion_test static_pages
after this i have write down this code in static_pages_soec,rb
require 'spec_helper'
describe "StaticPages" do
describe "Home page" do
it "should have the content 'sample app'" do
visit '/static_pages/home'
page.should have content('Sample App')
end
end
end
after that run this command but getting error
jaskaran#jaskaran-Vostro-1550:~/rails_project$ bundle exec rspec spec/requests/static pages spec.rb
/home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec
/core/configuration.rb:896:in `load': cannot load such file -- /home/jaskaran/rails_project/pages (LoadError)
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
Looks like there is a typo in your code.
page.should have content('Sample App')
should be
page.should have_content('Sample App')
If you are ever in doubt with Mike Hartl's tutorial you can always check the complete version here
The command you're running is incorrect.
You made a file called static_pages_spec.rb (at least I hope you did - your question is riddled with typos) but your command is looking for static pages spec.rb
Fix the command by adding the underscores.

Rspec generated spec failing

I've added the following to my application.rb file
config.generators do |g|
g.test_framework :rspec,
:fixtures => true,
:view_specs => false,
:helper_specs => false,
:routing_specs => false,
:controller_specs => true,
:request_specs => true
g.fixture_replacement :factory_girl, :dir => "spec/factories"
end
Then I generated a controller with
$rails g controller home index
This generated the specification in spec/controllers/home_controller_spec.rb which looks like this
describe HomeController do
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end
This is the default code generated by rSpec. I can see the page when I visit
http://localhost:3000/home/index
in my browser
Then when I run
$bundle exec rspec
I get this error
/Applications/MAMP/htdocs/2012/myapp/spec/controllers/home_controller_spec.rb:3:in `<top (required)>': uninitialized constant HomeController (NameError)
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194#myapp/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194#myapp/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194#myapp/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `map'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194#myapp/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load_spec_files'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194#myapp/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194#myapp/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:in `run'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194#myapp/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'
What does this error mean and how to I fix it?
EDIT: routes.rb has one line in it
get "home/index"
This error means that the spec file you run doesn't know about your HomeController class - it's not included.
I think you chose a more difficult way to use RSpec in Rails. The easiest way is to use rspec-rails. I looked into my controller specs and the first thing they do is to include rspec_helper.rb, which comes from rspec-rails. I guess this file is used to load all the Rails classes you need for your test.
To install rspec-rails all you need to do is add it to your Gemfile and run:
rails generate rspec:install

Migrating from Webrat to Capybara...unsuccessfully

Hoping someone might see what I've overlooked...
I'm trying to get Capybara working in a small existing application...and I'm not having any luck.
Gemfile:
group :development, :test do
gem 'rspec-rails'
# gem 'webrat'
gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
end
...
Similar specs in two places are failing for different reasons. Not sure why?
spec/controllers/pages_controller_spec.rb:
require 'spec_helper'
describe PagesController do
describe "GET 'about'" do
it "should be successful" do
# get 'about' #worked w/ webrat
# response.should be_success #worked w/ webrat
visit pages_about_path
# page.should have_content('About Us')
page.html.should match(/About/i)
end
it "should have title" do
# get 'about' #webrat
# response.should have_selector("title", :content => "About Us") #webrat
visit pages_about_path
page.should have_selector("title")
end
end
end
Failures:
(may be pulling in some generic page as doctype in browser is "<!DOCTYPE html>")
1) PagesController GET 'about' should be successful
Failure/Error: page.html.should match(/About/i)
expected "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n\n" to match /About/i
# ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
2) PagesController GET 'about' should have the right title
Failure/Error: page.should have_selector("title")
expected css "title" to return something
# ./spec/controllers/pages_controller_spec.rb:20:in `block (3 levels) in <top (required)>'
spec/views/pages/about.html.haml_spec.rb:
require 'spec_helper'
describe "pages/about.html.haml" do
it "renders attributes in <p>" do
# render #webrat
# rendered.should match(/About/) #webrat
visit pages_about_path
page.should have_content("About Us")
end
it "should have the right heading" do
# render #webrat
# rendered.should have_selector("h2", :content => "About Us") #webrat
visit pages_about_path
page.should have_selector("h2")
end
end
Failures:
1) pages/about.html.haml renders attributes in <p>
Failure/Error: visit pages_about_path
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000101dc2970>
# ./spec/views/pages/about.html.haml_spec.rb:8:in `block (2 levels) in <top (required)>'
2) pages/about.html.haml should have the right heading
Failure/Error: visit pages_about_path
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x000001034b1d98>
# ./spec/views/pages/about.html.haml_spec.rb:16:in `block (2 levels) in <top (required)>'
Just had the same issue and found that capybara needs:
response.body.should have_selector("title")
webrat does not need the .body
also, make sure you are rendering views eg:.
describe PagesController do
render_views
You should include capybara DSL in your test file :
require 'spec_helper'
describe PagesController do
include Capybara::DSL
describe "GET 'about'" do
it "should be successful" do
...
This won't fix all your problems, but Instead of:
page.html.should match(/About/i)
try:
page.should match(/About/i)
(you might not even need page).
Also, check that you have Capybara set to use CSS queries in env.rb (it defaults to XPath).
Ensure that your spec_helper.rb file has the following at the top:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
I had a similar issue as yours (i.e., undefined method 'visit') and popping those lines solved the issue.

Resources