查询参数和 assert_generates/assert_routing - 我错过了什么? [英] Query parameters and assert_generates/assert_routing - what am I missing?

查看:41
本文介绍了查询参数和 assert_generates/assert_routing - 我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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',
                     { :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 测试通过 - 我错过了什么?

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

推荐答案

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

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.

尝试类似的方法:

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

这篇关于查询参数和 assert_generates/assert_routing - 我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆