5

I'm trying to test my Rails 3.2.3 application by RSpec. It's installed (I already tested another application and it worked well) but it doesn't exist in Gemfile. Here is the code of spec/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'

The file ApplicationController.rb

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

end

Even when I add gem 'rspec' in Gemfile it won't change anything, the error will remain.

Any thoughts?

1

3 Answers 3

7

In my case I needed to add rails_helper either to the spec file or the .rspec file.

require 'spec_helper'
require 'rails_helper'

describe SomeController do
end

# Or in the .rspec file:
--color
--require spec_helper
--require rails_helper
Sign up to request clarification or add additional context in comments.

Comments

6

What is this require_relative doing there? I use rspec and don't need this. Actually the only require in my spec files is:

require 'spec_helper'

(At least in most cases, there are a few files that require special stuff like "authlogic/test_case")

My Gemfile entry for rspec looks like this:

group :development do
  gem 'rspec-rails'
end

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

You won't need spork if you don't use it of course, but maybe the "rspec-rails" instead "rspec" could be your problem.

4 Comments

The error is require: cannot load such file -- spec_helper (LoadError)
Did you run "rails generate rspec:install" ? Does the file exist in the spec folder?
Did accepting my answer mean that it was helping? Or is it still not running? (The question: Does the file exist in the spec folder? Was about the spec_helper file. If that's not there, then something went wrong with rspec setup on a basic level)
Your answer is what I was looking for. Now it is working well.
1

I had the same issue, what resolved it was to ensure that the spec file was named exactly the same as the controller file. So no controller_name_controller_spec.rb but just controller_name_controller.rb.

1 Comment

wrong, test won't execute if you remove the 'spec' postfix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.