查询参数和assert_generates / assert_routing - 我错过了什么?

时间:2011-07-12 14:25:00

标签: ruby-on-rails testing

我想我已经介绍了使用查询参数测试路由的排列,但没有一种方法通过。

在我的routes.rb中,我有以下内容:

resources :items

然后我的功能测试我有:

require 'ruby-debug'
require 'test_helper'

class ItemsControllerTest < ActionController::TestCase
  # Failure: test_assert_generates_using_params_and_extras(ItemsControllerTest) [test/functional/items_controller_test.rb:7]: The generated path <"/items/1/edit"> did not match <"/items/1/edit?q=abc">
  test "assert_generates using params and extras" do
    assert_generates '/items/1/edit?q=abc',
                     { :controller => 'items', :action => 'edit', :id => '1', :q => 'abc' },
                     {},
                     { :q => 'abc' }
  end

  # Failure: test_assert_generates_using_only_params(ItemsControllerTest) [test/functional/items_controller_test.rb:15]: found extras <{:q=>"abc"}>, not <{}>
  test "assert_generates using only params" do
    assert_generates '/items/1/edit?q=abc',
                     { :controller => 'items', :action => 'edit', :id => '1', :q => 'abc' }
  end

  # Failure: test_assert_generates_using_using_only_extras(ItemsControllerTest) [test/functional/items_controller_test.rb:21]: found extras <{}>, not <{:q=>"abc"}>
  test "assert_generates using using only extras" do
    assert_generates '/items/1/edit?q=abc',
                     { :controller => 'items', :action => 'edit', :id => '1' },
                     {},
                     { :q => 'abc' }
  end

  # Failure: test_assert_routing_using_params_and_extras(ItemsControllerTest) [test/functional/items_controller_test.rb:29]: The generated path <"/items/1/edit"> did not match <"/items/1/edit?q=abc">
  test "assert_routing using params and extras" do
    assert_routing '/items/1/edit?q=abc',
                     { :controller => 'items', :action => 'edit', :id => '1', :q => 'abc' },
                     {},
                     { :q => 'abc' }
  end

  # Failure: test_assert_routing_using_only_params(ItemsControllerTest) [test/functional/items_controller_test.rb:37]: found extras <{:q=>"abc"}>, not <{}>
  test "assert_routing using only params" do
    assert_routing '/items/1/edit?q=abc',
                     { :controller => 'items', :action => 'edit', :id => '1', :q => 'abc' }
  end

  # Failure: test_assert_routing_using_using_only_extras(ItemsControllerTest) [test/functional/items_controller_test.rb:43]: found extras <{}>, not <{:q=>"abc"}>  
  test "assert_routing using using only extras" do
    assert_routing '/items/1/edit?q=abc',
                     { :controller => 'items', :action => 'edit', :id => '1' },
                     {},
                     { :q => 'abc' }
  end
end

我本来期望* _using_params_and_extras测试通过 - 我错过了什么?

1 个答案:

答案 0 :(得分:4)

也许你已经弄明白了。我今天遇到了同样的问题,直到我发现了这个问题,注意到了额外选项的存在,最后再次阅读了assert_routing的文档。你必须从url中删除q = abc并将它们放在extras hash和options hash中。

所以尝试类似:

assert_routing(
  'items/1/edit',
  {:controller => 'items', :action => 'edit', :id => '1', :q => 'abc'},
  {},
  {:q => 'abc'}
)