为 Rails 3 插件生成测试路由时出错?
我正在尝试为插件“foobar”开发测试,该插件修改了一些标准 Rails 助手。在vendor/plugins/foobar/test/foobar_test.rb中,我有以下内容:
# create the test model
class Thing < ActiveRecord::Base
end
# create the test controller, which renders the included index template
class ThingsController < ActionController::Base
def index
@things = Thing.all
format.html { render(:file => 'index') }
end
def destroy
@thing = Thing.find(params[:id])
@thing.destroy
format.html { render(:file => 'index') }
end
end
# confirm that the test environment is working correctly
class ThingsTest < ActiveSupport::TestCase
test "model is loaded correctly" do
assert_kind_of Thing, Thing.new
end
end
# confirm that the controller and routes are working correctly
class ThingsControllerTest < ActionController::TestCase
test "should load index" do
with_routing do |set|
set.draw do
resources :things, :only => [:index, :destroy]
end
get :index
assert_response :success
end
end
end
当我运行rake时,我得到以下输出:
test_should_load_index(ThingsControllerTest):
NameError: undefined local variable or method `_routes' for ThingsController:Class
知道我做错了什么导致我无法访问路由吗?我已经研究了好几天并搜索了很多文档,但无济于事。感谢您的帮助!
I'm trying to develop tests for plugin "foobar" that modifies some of the standard Rails helpers. In vendor/plugins/foobar/test/foobar_test.rb, I have the following:
# create the test model
class Thing < ActiveRecord::Base
end
# create the test controller, which renders the included index template
class ThingsController < ActionController::Base
def index
@things = Thing.all
format.html { render(:file => 'index') }
end
def destroy
@thing = Thing.find(params[:id])
@thing.destroy
format.html { render(:file => 'index') }
end
end
# confirm that the test environment is working correctly
class ThingsTest < ActiveSupport::TestCase
test "model is loaded correctly" do
assert_kind_of Thing, Thing.new
end
end
# confirm that the controller and routes are working correctly
class ThingsControllerTest < ActionController::TestCase
test "should load index" do
with_routing do |set|
set.draw do
resources :things, :only => [:index, :destroy]
end
get :index
assert_response :success
end
end
end
When I run rake, I get the following output:
test_should_load_index(ThingsControllerTest):
NameError: undefined local variable or method `_routes' for ThingsController:Class
Any idea what I'm doing wrong that is preventing me from accessing the routes? I've been at this for days and scoured much documentation, to no avail. Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过遵循 "创建的基础知识,在 Rails 3.1.0.rc1 和 Ruby 1.9.2 上工作Rails 插件” Rails 指南,并在代码因不兼容而崩溃时更正代码。
我首先运行:
然后修改生成的代码以获取除了默认文件之外的这些文件:
最后,我们的测试通过:
I got things to work on Rails 3.1.0.rc1 with Ruby 1.9.2, by following "The Basics of Creating Rails Plugins" Rails Guide and correcting the code whenever it exploded due to an incompatibility.
I started by running:
Then modifying the generated code to obtain these files in addition to the default ones:
And finally, our test passes:
调试此问题的最佳选择是这样做,
这将使您更好地了解导致 _routes 错误的原因(即行号)。
Your best bet for debugging this is to do
This will give you a much better idea (i.e. a line number) on what is causing the _routes error.