Testing with RSpec in a RoR Application - Uninitialized Constant

I am trying to test a Rails 3.2.3 application from RSpec. It was installed (I already tested another application, and it worked well), but it does not exist in the Gemfile. Here is the codespec/application_controller_spec.rb

    require "rspec"
    require_relative "../app/controllers/application_controller"

    describe ApplicationController do
      it "current_cart does something" do
        #app_controller  = ApplicationController.new
        pending
      end
    end

The following command returns an error:

alex@ubuntu:~/RubymineProjects/psg$ rspec spec
/home/alex/RubymineProjects/psg/app/controllers/application_controller.rb:1:in `<top (required)>': uninitialized constant ActionController (NameError)
    from /home/alex/RubymineProjects/psg/spec/application_controller_spec.rb:2:in `require_relative'
    from /home/alex/RubymineProjects/psg/spec/application_controller_spec.rb:2:in `<top (required)>'
    from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
    from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
    from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
    from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
    from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
    from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
    from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

File ApplicationController.rb

 class ApplicationController < ActionController::Base
   def some_action
     #............
   end

end

Even when I add gem 'rspec'to the Gemfile, it will not change anything, the error will remain.

Any thoughts?

+5
source share
3 answers

What is it for? I use rspec and do not need it. In fact, only in my spec files:

require 'spec_helper'

(At least in most cases there are several files that require special materials, such as "authlogic / test_case")

Gemfile rspec :

group :development do
  gem 'rspec-rails'
end

group :test do
  gem 'rspec-rails'
  gem 'spork', '0.9.0.rc8'
end

spork, , , , "rspec-rails" "rspec" .

+6

rails_helper , .rspec.

require 'spec_helper'
require 'rails_helper'

describe SomeController do
end

# Or in the .rspec file:
--color
--require spec_helper
--require rails_helper
+7

I had the same problem that was solved to ensure that the spec file was named exactly the same as the controller file. Therefore, there is no control_name_controller_spec.rb, but just controller_name_controller .rb.

+1
source

All Articles