Ruby on rails Rails rspec未定义的局部变量或方法'render#u views';
运行rspec时,我得到: C:/www/kill/spec/games\u controller\u spec.rb:4:inRuby on rails Rails rspec未定义的局部变量或方法'render#u views';,ruby-on-rails,rspec,Ruby On Rails,Rspec,运行rspec时,我得到: C:/www/kill/spec/games\u controller\u spec.rb:4:inblock-in':未定义的局部变量或方法render\u视图' 游戏\控制器\规格rb spec_helper.rb 宝石: 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 您应该将控制器规格放在spec/controlle
block-in':未定义的局部变量或方法
render\u视图'
游戏\控制器\规格rb
spec_helper.rb
宝石:
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
您应该将控制器规格放在
spec/controllers
目录中。RSpec基于此包括这些方法,AFAIK。您的控制器规格应位于spec/controllers/
文件夹中。但是,出于某种原因,您可能希望有一些其他文件运行控制器规范(例如,我正在一个单独的测试文件中测试标记验证)
您可以这样做:
describe GamesController, "creating a new game", :type => :controller do
这应该能奏效 如果您测试视图,RSpec将拥有该视图的
spec/requests
目录,默认情况下它将呈现视图。()此问题可能是由Gem rspec rails版本引起的。将其降级回2.x解决了我的问题:
# edit your Gemfile
gem "rspec-rails", "2.14.2"
在我的例子中,我的测试已经在
spec/controllers/
中了。我需要添加config.infere\u spec\u type\u from\u file\u location代码>在spec|u helper.rb中的RSpec.configure do | config |
循环中。我将rspec从2.x更新到3.x,但没有意识到这一点,而且3.x中的控制器测试需要配置。谢谢,这是我的问题。对我来说,另一个问题是我没有在spec_helper.rb中“需要'rspec/rails'”。这对我来说很有效。升级rspec之后,我需要将type::controller
添加到descripe块,即使它位于正确的目录结构中。如图所示……请注意,require'spec\u helper'
语法自RSpec 3.0以来已被弃用,并已被require'rails\u helper'
取代(因此您必须重命名相应的文件)。我对spec/views/…
和@FabriceCarrega提到的解决方案也有类似的问题,将spec\u helper
更改为rails\u helper
。谢谢
describe GamesController, "creating a new game", :type => :controller do
# edit your Gemfile
gem "rspec-rails", "2.14.2"