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
Related
Rails 5.0.0.1
Rspec 3.5.4
Ruby 2.3.1
We have been trying to provide test coverage for our rails application. We have a rescue in a private method that Rspec is not reaching.
Rspec:
it 'returns 200 after 404 from GET #edit error' do
allow(controller).to receive(:getpackages).and_return(URI::InvalidURIError)
expect(response.code).to eq(200) # => covers the 200
expect(response).to render_template('errors/5xx') # => doesn't read
end
Rails:
private
def set_package
#package = PackageServices.getpackage params[:id]
rescue URI::InvalidURIError
render 'errors/5xx'
end
Error message:
expecting <"errors/5xx"> but rendering with <[]>
./spec/controllers/packages_controller_spec.rb:139:in `block (3 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
We have tried to assert_template, tried to stub it using stub_template, installed a gem rails-controller-testing (not rspec), but we have run out of ideas and every google link is purple. Is this a bug in Rspec or are we going about it the wrong way?
I believe the stabbing was incorrect. Try the following code, it should work.
context 'URI is invalid' do
before do
allow(PackageServices).toreceive(:getpackage).and_raise(URI::InvalidURIError)
end
it 'returns 200 after 404 from GET #edit error' do
expect(response.code).to eq(200) # => covers the 200
expect(response).to render_template('errors/5xx') # => doesn't read
end
end
I have a rails App. I have this file app/errors/status.rb
I try to pass the test but is not not working.
module Errors
class Status
def initialize status
#status = status
end
def default_message
"Error in the server status: #{status}"
end
private
attr_reader :status
end
end
And the test on spec/errors/status_spec.rb:
require 'rails_helper'
describe Errors::Status do
let(:status) { double 'status' }
subject { described_class.new status }
describe 'default_message' do
it 'returns the default message' do
expect(subject.call).to eq( "Error in the server status: #{status}")
end
end
end
And it keeps throwing this error:
/Users/gerardmorera/bet_play/spec/errors/status_spec.rb:3:in `<top (required)>': uninitialized constant Errors (NameError)
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
That’s because of how ActiveSupport’s auto-loading works and the way in which Rails sets up the $LOAD_PATH. Autoload sees Errors::Status and expects to find it at errors/status somewhere via require, but it doesn’t because app/errors is in the $LOAD_PATH, so you would require your file with just require 'errors'.
You can fix this by moving app/errors/status.rb to a location ActiveSupport’s auto-loading expects (e.g. app/<something>/errors/status.rb). You can puts $LOAD_ATH to see all the possible locations (note that Rails will add all directories in app/ to the $LOAD_PATH).
I am new to Rspec and am trying to test the one route in my application. I have installed Rspec and have included the routing file in spec/routing/routes_spec.rb.
My spec is as follows:
require "spec_helper"
describe "Routes" do
it "routes get index" do
expect(:get => "simulations").to route_to(
:controller => "simulations",
:action => "index"
)
end
end
I get this error:
Routes routes get index
Failure/Error: expect(:get => "simulations").to route_to(
NoMethodError:
undefined method `route_to' for #<RSpec::ExampleGroups::Routes:0x007fc32d2f70b8 #__memoized=nil>
# ./spec/routing/routes_spec.rb:6:in `block (2 levels) in <top (required)>'
Any ideas as to why route_to would be undefined? I have verified that the route actually works.
In Rspec 3 you should require 'rails_helper' rather than require 'spec_helper'.
Based on documentation:
Routing specs are marked by :type => :routing or if you have set config.infer_spec_type_from_file_location! by placing them in spec/routing.
So, unless you set the previous option, you should begin your spec with:
describe "Routes", :type => :routing do
As Fire-Dragon-DoL suggests above, you might like to check that the rspec-rails gem is in place.
When you don't have rspec-rails installed and required, and you use to_route method, you will get the same error when running your specs: NoMethodError: undefined method 'route_to'
Given the same setup, when you use be_routable matcher, then you get another error, in the style of: expected {:get=>"/my_models/} to respond to 'routable?'
To remedy these errors
Add rspec-rails to Gemfile
Run bundle install
Add require 'rspec/rails' to spec_helper (or rails_helper)
I'm trying to include some helpers to my test but I can't make that it works.
I got the following error:
/home/edu/.rvm/rubies/ruby-1.9.3-p392/bin/ruby -S rspec ./spec/features/customers_spec.rb ./spec/features/login_spec.rb ./spec/features/products_spec.rb ./spec/features/suppliers_spec.rb
/home/edu/Desktop/rails_proyects/gg/spec/support/features.rb:2:in `block in <top (required)>': uninitialized constant MyHelp (NameError)
from /home/edu/.rvm/gems/ruby-1.9.3-p392#gg/gems/rspec-core-2.14.6/lib/rspec/core.rb:120:in `configure'
from /home/edu/Desktop/rails_proyects/gg/spec/support/features.rb:1:in `<top (required)>'
I have this:
# spec/support/features/session_helper.rb
module MyHelp
module SessionHelpers
...
def sign_in
...
end
end
end
# spec/support/features.rb
RSpec.configure do |config|
config.include MyHelp::SessionHelpers, type: :feature
end
I'm using it here:
# spec/features/login_spec.rb
require 'spec_helper'
feature "Login" do
scenario "with valid credentials" do
user = create(:user)
sign_in user.email, user.password
page.should have_content(I18n.t('layouts.header.exit', locale: 'es'))
end
end
I'm using:
rspec (2.14.1)
rspec-core (2.14.6, 2.14.5)
rspec-expectations (2.14.3, 2.14.2)
rspec-mocks (2.14.4, 2.14.3)
rspec-rails (2.14.0)
ruby 1.9.3p392
rails 3.2.13
Can someone help me with this?
thank you.
It looks like you just need to require the new helper before you try to use it in spec/support/features.rb
require Rails.root.join('spec/support/features/session_helper')
Also, it's best practice to have your class/module match the file name, so either the file should be pluralized, or the helper singularized.
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'