Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails rspec undefined local variable or method `render_views'

When running the rspec I get:

C:/www/kill/spec/games_controller_spec.rb:4:in block in <top (required)>': undefined local variable or methodrender_views'

games_controller_spec.rb

require 'spec_helper'

describe GamesController, "creating a new game" do
  render_views
  fixtures :games

  it "should redirect to index with a notice on successful save" do
    Game.any_instance.stubs(:valid?).returns(true)
    post 'create'
    assigns[:game].should_not be_new_record
    flash[:notice].should_not be_nil
    response.should redirect_to(menu_items_path)
  end
end

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # == Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  #config.mock_with :rspec

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false
end

gems:

C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-2.7.0

C:/Ruby192/lib/ruby/gems/1.9.1/gems/mocha-0.10.0

like image 297
João Soares Avatar asked Nov 06 '11 18:11

João Soares


3 Answers

Your controller specs should be in the spec/controllers/ folder. However, you may want for some reason to have some other files that run controller specs (for example, I'm testing markup validation in a separate test file).

You can do this :

describe GamesController, "creating a new game", :type => :controller do

And this should do the trick !

like image 165
Fabrice Carrega Avatar answered Nov 09 '22 02:11

Fabrice Carrega


You should place your controller specs inside a spec/controllers directory. RSpec is including these methods based on this, AFAIK.

like image 30
Simon Perepelitsa Avatar answered Nov 09 '22 02:11

Simon Perepelitsa


In my case my test was already in spec/controllers/. I needed to add config.infer_spec_type_from_file_location! inside the RSpec.configure do |config| loop in spec_helper.rb. I had updated rspec from 2.x to 3.x without realizing it, and that config is needed for controller tests from 3.x.

like image 5
Shane E. Avatar answered Nov 09 '22 03:11

Shane E.