开发者

Query parameters and assert_generates/assert_routing - what am I missing?

开发者 https://www.devze.com 2023-03-19 11:59 出处:网络
I think I\'ve covered the permutations for testing a route with a query parameter, but none of the approaches passes.

I think I've covered the permutations for testing a route with a query parameter, but none of the approaches passes.

In my routes.rb I have the following:

resources :items

Then for my functional test I have:

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',
 开发者_Python百科                    { :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

I would have expected the *_using_params_and_extras tests to pass - what am I missing?


Maybe you figure this out already. I had the same problem today until I found this question, noticed the presence of the extra options and finally read the documentation for assert_routing again. You gotta remove the q=abc from the url and put it both in the extras hash and the options hash.

So try something like:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号