Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSpec控制器测试将哈希转换为字符串_Ruby On Rails_Ruby_Hash_Rspec_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails RSpec控制器测试将哈希转换为字符串

Ruby on rails RSpec控制器测试将哈希转换为字符串,ruby-on-rails,ruby,hash,rspec,ruby-on-rails-5,Ruby On Rails,Ruby,Hash,Rspec,Ruby On Rails 5,我的应用程序有一个控制器规范,它在控制器上测试create方法。创建操作实际上可以正常工作,但是规范失败了。它似乎在自动将hash POST参数转换为字符串 let(:coupon) { attributes_for(:coupon) } describe 'POST #create' do it 'should create a new coupon from params' do expect { post :create, :coupon => coupon

我的应用程序有一个控制器规范,它在控制器上测试create方法。创建操作实际上可以正常工作,但是规范失败了。它似乎在自动将hash POST参数转换为字符串

let(:coupon) { attributes_for(:coupon) }

describe 'POST #create' do
  it 'should create a new coupon from params' do
    expect {
      post :create, :coupon => coupon
    }.to change(Coupon, :count).by(1)
  end
end
现在,如果我做
put优惠券
,它将生成一个有效的数据散列,类型是散列。出于某种原因,控制器正在接收参数[:优惠券]的字符串。只有在rspec测试中才会发生这种情况,当我使用POST表单在browse中进行测试时,它工作得非常好

Rspec抛出以下消息:

NoMethodError:
   undefined method `permit' for #<String:0x00000005062700>
   Did you mean?  print
命名错误:
未定义的方法“许可证”#
你是说?打印
如果我在rspec中的控制器中
放置params[:优惠券].class
,它会给我字符串。为什么会将我的散列转换为POST请求的字符串,我如何防止这种情况发生


我使用的是Rails 5.0.0和rspec 3.5.1

最近在测试JSON API端点时,我发现了完全相同的行为。最初我的主题是:

subject{put:my_endpoint,**input_args}
input_args
中的整数值被转换为字符串。修复方法是添加
格式:“json”
作为
put
的附加关键字参数:

subject{put:my_endpoint,**输入参数,格式:'json'}

这似乎是gem
open\u淘宝在测试中以某种方式转换我的post请求的问题。

不幸的是,这没有解决,我已经尝试使用各种JSON函数对其进行测试。我的整个优惠券散列正在转换为字符串。在控制器中,当使用params.require(:优惠券)时,它获取的是字符串而不是哈希,因此它无法访问哈希的任何值。