4

我在打开资产管道的轨道 3.2 上。我正在测试我尚未构建的操作(TDD;试图让测试首先失败)。当我最初运行测试时,我得到了预期的失败。

class AccountsControllerTest < ActionController::TestCase

  def test_my_path
    get :my_path
    puts @response.body
    assert_template :my_path
  end

end

#=> test_my_path(AccountsControllerTest): AbstractController::ActionNotFound: The action 'my_path' could not be found for AccountsController

当我添加相应的视图 (app/views/my_path.html.erb) 时,我仍然认为测试会失败,因为我没有为此操作指定路线。它通过了,我认为这是因为页面正在由资产管道呈现。在我调用的视图中,它从调用<%= request.fullpath %>中吐出。/assets?action=my_pathputs @response.body

当我尝试在浏览器中访问 accounts/my_path 时,我看到“No route matches [GET] "/accounts/my_path"”,所以我想确保我的测试也失败了。为什么会发生这种情况,我应该如何修复测试?我是否应该使用 assert_recognizes 单独测试路由?为了缩小问题的根源,我的路线文件目前是空的。

4

1 回答 1

1

首先,即使没有定义相应的操作,Rails 控制器也会呈现现有模板 ( http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-by-default-convention-over-configuration-in-action )。这就是添加模板后测试通过的原因。

功能测试直接调用控制器动作,不经过路由器。因此,即使未定义路由,测试也会通过,并且在浏览器中不起作用。使用单独的测试用例进行路由测试(或在集成测试中测试路由)。

于 2012-08-06T15:02:48.107 回答