Reputation: 55
I keep getting this error when trying to run a Rspec test as part of the Rubyonrails tutorial
Failures:
1) GET 'about' should be successful
Failure/Error: get 'about'
RuntimeError:
@controller is nil: make sure you set it in your test's setup method.
# ./spec/controllers/pages_controller_spec.rb:27:in `block (2 levels) in <top (required)>'
I have tried everything I can to try to correct it but all have proven futile. Any help will be appreciated.
Here is my spec_helper.rb file
require 'rubygems'
require 'spork'
Spork.prefork do
end
Spork.each_run do
end
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.include RSpec::Rails::ControllerExampleGroup
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
pages_controller.rb file:
class PagesController < ApplicationController
def home
end
def contact
end
def about
end
end
pages_controller_spec.rb file:
require 'spec_helper'
describe PagesController do
render_views
describe "GET 'home'" do
render_views
it "should be successful" do
get 'home'
response.should be_success
end
end
describe "GET 'contact'" do
render_views
it "should be successful" do
get 'contact'
response.should be_success
end
end
end
describe "GET 'about'" do
render_views
it "should be successful" do
get 'about'
response.should be_success
end
end
routes.rb file:
SampleApp::Application.routes.draw do
get "pages/home"
get "pages/contact"
get "pages/about"
end
btw I have no problem running Spork and autotest.
Upvotes: 3
Views: 3945
Reputation: 27374
The problem is the nesting in your tests: the describe PagesController
block only wraps the first two tests and not the last one.
It should look like this:
describe PagesController do
render_views
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
end
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
end
describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
end
end
See: How do i get rid of @controller is nil error in my tests
p.s. I've taken out all the render_views
except the one in the top-level describe
block -- you should only need to call it once for the block (see the specs).
Upvotes: 6