我正在使用Rails中的minitest进行测试,但我遇到了一个问题,我希望一位经验丰富的测试人员可以帮助我,因为我已经尝试到处寻找答案,但它没有似乎任何人都遇到了这个问题,或者如果有的话,他们选择了集成测试。
假设我有一个名为Foo
的控制器,其中的操作名为bar
。所以foo_controller.rb
文件看起来像这样:
class FooController < ApplicationController
def bar
render 'bar', :layout => 'application'
end
end
问题是我不希望人们直接访问“foo / bar”路径。所以我的路线是get 'baz' => 'foo#bar'
。
现在我要测试FooController:
require 'minitest_helper'
class FooControllerTest < ActionController::TestCase
def test_should_get_index
get '/baz'
end
end
但是测试导致错误No route matches {:controller=>"foo", :action=>"/baz"}
。如何为GET请求指定控制器?
对不起,如果这是一个愚蠢的问题。我很难找到答案。
答案 0 :(得分:2)
尝试测试:
使用Rails 3:
require 'minitest_helper'
class FooControllerTest < ActionController::TestCase
def test_should_get_index
assert_routing "/baz", :controller => "foo", :action => "bar"
end
end
使用Rails 4:
require "test_helper"
class FooControllerTest < ActionDispatch::IntegrationTest
def test_should_get_index
assert_generates "/baz", :controller => "foo", :action => "bar"
end
end