运行rspec时我得到:
C:/www/kill/spec/games_controller_spec.rb:4:in
block in <top (required)>': undefined local variable or method
render_views'
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
# 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
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
答案 0 :(得分:15)
您的控制器规格应位于spec/controllers/
文件夹中。但是,您可能由于某种原因需要运行控制器规范的其他文件(例如,我在单独的测试文件中测试标记验证)。
你可以这样做:
describe GamesController, "creating a new game", :type => :controller do
这应该可以解决问题!
答案 1 :(得分:3)
就我而言,我的测试已经在spec/controllers/
。我需要在spec_helper.rb中的config.infer_spec_type_from_file_location!
循环中添加RSpec.configure do |config|
。我在没有意识到的情况下将rspec从2.x更新到3.x,并且从3.x进行控制器测试需要配置。
答案 2 :(得分:2)
您应将控制器规格放在spec/controllers
目录中。 RSpec基于此包括这些方法,AFAIK。
答案 3 :(得分:0)
如果您测试视图,RSpec有spec/requests
目录,默认情况下它将呈现视图。 (See the doc)
答案 4 :(得分:0)
此问题可能是由您的Gem rspec-rails版本引起的。将它降级回2.x解决了我的问题:
# edit your Gemfile
gem "rspec-rails", "2.14.2"