ruby-on-rails – 我的存根上的rspec put请求的正确参数是什么

ruby-on-rails – 我的存根上的rspec put请求的正确参数是什么,第1张

概述我有一个控制器规格,我得到了以下失败的期望: Failure/Error: put :update, :id => login_user.id, :user => valid_attributes #<User:0xbd030bc> received :update_attributes with unexpected arguments expected: ({:name=>"c 我有一个控制器规格,我得到了以下失败的期望:

Failure/Error: put :update,:ID => login_user.ID,:user => valID_attributes   #<User:0xbd030bc> received :update_attributes with unexpected arguments     expected: ({:name=>"changed name",:email=>"changed@mail.com",:password=>"secret",:password_confirmation=>"secret"})          got: ({"name"=>"Test user","email"=>"user@test.com","password"=>"secret","password_confirmation"=>"secret"})

对我来说,看起来我正在传递“name”=> “测试用户”,我期待:name => “测试用户”

我的规格看起来像这样:

describe 'with valID parameters' do      it 'updates the user' do       login_user = User.create!(valID_attributes)       controller.stub(:current_user).and_return(login_user)       User.any_instance.          should_receive(:update_attributes).          with(valID_attributes.merge(:email => "changed@mail.com",:name=>"changed name"))       put :update,:user => valID_attributes      end end

我的有效属性有这样的东西:

def valID_attributes  {    :name => "Test user",:email=> "user@test.com",:password => "secret",:password_confirmation => "secret"  }end

所以我的参数有什么问题吗?

我使用Rails 3.0.5与rspec 2.6.0 …

解决方法 失败消息告诉您到底发生了什么:User的任何实例都期望update_attributes带有一个哈希,包括:email => “changed@mail.com”,但它得到了:email => “user@test.com”因为那是valID_attributes中的内容.同样,它期望:name => “changed_name”,但得到:name => “测试用户”,因为这是valID_attributes中的内容.

您可以简化此示例并避免这种混淆.这里不需要使用valID_attributes,因为无论如何,should_receive会拦截update_attributes调用.我通常这样做:

controller.stub(:current_user).and_return(mock_model(User)) # no need for a real user hereUser.any_instance.  should_receive(:update_attributes).  with({"these" => "params"})put :update,:user => {"these" => "params"}

这样,预期值和实际值在示例中都是正确的,并且它清楚地表明它们的含义并不重要:无论传递的是什么样的哈希:用户直接传递给update_attributes.

合理?

总结

以上是内存溢出为你收集整理的ruby-on-rails – 我的存根上的rspec put请求的正确参数是什么全部内容,希望文章能够帮你解决ruby-on-rails – 我的存根上的rspec put请求的正确参数是什么所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1286210.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存