ruby-on-rails – 我的存根上的rspec put请求的正确参数是什么
发布时间:2020-12-17 02:52:28 所属栏目:百科 来源:网络整理
导读:我有一个控制器规格,我得到了以下失败的期望: 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
我有一个控制器规格,我得到了以下失败的期望:
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 here User.any_instance. should_receive(:update_attributes). with({"these" => "params"}) put :update,:user => {"these" => "params"} 这样,预期值和实际值在示例中都是正确的,并且它清楚地表明它们的含义并不重要:无论传递的是什么样的哈希:用户直接传递给update_attributes. 合理? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |