rspec 测试控制器后将我的参数从符号更改为字符串并破坏我的测试 [英] rspec testing a controller post changing my params from symbols to strings and breaking my tests

查看:28
本文介绍了rspec 测试控制器后将我的参数从符号更改为字符串并破坏我的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器规范中,我这样做:

In my controller spec I am doing this:

it "should create new message" do
  Client.should_receive(:create).with({:title => 'Mr'})
  post 'create' , :client => {:title => "Mr" }
end

...在我的控制器中我正在做...

... and in my controller I am doing ...

def create
  client = Client.create(params[:client])
end

但是这会失败并显示以下错误消息:

However this is failing with the following error message :

  expected: ({:title=>"Mr"})
       got: ({"title"=>"Mr"})

我想知道为什么会发生这种情况以及如何让它发挥作用

I'm wondering why this is happening and how to get it to work

推荐答案

这是因为您传递的是符号而不是字符串.这应该解决它:

It's because you are passing a symbol and not a string. This should fix it :

it "should create new message" do
  Client.should_receive(:create).with({:title => 'Mr'})
  post 'create' , :client => {"title" => "Mr" }
end

这是一篇关于它的博文:了解 Ruby 符号"

Here's a blogpost about it: "Understanding Ruby Symbols"

这篇关于rspec 测试控制器后将我的参数从符号更改为字符串并破坏我的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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