如何使用xhr在rspec中使用“ params”键发布JSON?

时间:2019-03-19 09:28:49

标签: ruby-on-rails rspec xmlhttprequest

使用xhr测试控制器时遇到问题。

 it 'send request' do
    do_login :superguru

    xhr :post, :create,
        locale: :en,
        customer_id: customer.id,
        ...
        params: 'some,key'
  end

堆栈跟踪:

 # /Users/admin/.rvm/gems/ruby-2.4.4@energo/gems/actionpack-4.2.11/lib/action_dispatch/routing/route_set.rb:802:in merge!'
 # /Users/admin/.rvm/gems/ruby-2.4.4@energo/gems/actionpack-4.2.11/lib/action_dispatch/routing/route_set.rb:802:in url_for'
 # /Users/admin/.rvm/gems/ruby-2.4.4@energo/gems/actionpack-4.2.11/lib/action_dispatch/routing/route_set.rb:773:in `path_for'
 # /Users/admin/.rvm/gems/ruby-2.4.4@energo/gems/actionpack-4.2.11/lib/action_controller/test_case.rb:745:in `build_request_uri'
 # /Users/admin/.rvm/gems/ruby-2.4.4@energo/gems/actionpack-4.2.11/lib/action_controller/test_case.rb:634:in `process'
 # ./spec/spec_helper.rb:41:in `process_with_default_locale'
 # /Users/admin/.rvm/gems/ruby-2.4.4@energo/gems/actionpack-4.2.11/lib/action_controller/test_case.rb:67:in `process'
 # /Users/admin/.rvm/gems/ruby-2.4.4@energo/gems/actionpack-4.2.11/lib/action_controller/test_case.rb:520:in `post'
 # /Users/admin/.rvm/gems/ruby-2.4.4@energo/gems/actionpack-4.2.11/lib/action_controller/test_case.rb:550:in `xml_http_request'
 # ./spec/controllers/api/meta/customers/data_series_import_configurations_controller_spec.rb:40:in `block (3 levels) in <module:Customers>'

问题在/Users/admin/.rvm/gems/ruby-2.4.4@energo/gems/actionpack-4.2.11/lib/action_dispatch/routing/route_set.rb:802中,因为存在

if options.key? :params
   params.merge! options[:params]
end

options[:params] =='some,key',它是一个字符串。当我从请求中删除params时,它通过了。同样,当我将params的值更改为Hash时。

但是我确实需要使用params键传递字符串。知道怎么做吗?

2 个答案:

答案 0 :(得分:2)

尝试rails-5样式:

xhr :post, :create, params: { locale: :en, ... params: 'some,key' }

答案 1 :(得分:0)

第二个选择是使用params类型的String键,而不是Symbol