【问题标题】:Rspec has_one relation error when updating更新时Rspec has_one关系错误
【发布时间】:2015-10-16 04:52:19
【问题描述】:

我有用户(我使用了 Devise)和 Profile 模型,其中 User has_one Profile 作为他们的关系。运行 rspec 测试时出现错误。以下是我在用户更新他/她的个人资料时要处理的规范。

spec/controller/profiles_controller_spec.rb

RSpec.describe ProfilesController, type: :controller do
  let(:profile) { FactoryGirl.create(:profile) }
  let (:valid_attributes) { FactoryGirl.attributes_for(:profile) }
  let (:invalid_attributes) { FactoryGirl.attributes_for(:profile).merge({fname: nil}) }
  let(:user) { FactoryGirl.create(:user) }
  let(:valid_session) { sign_in(user) }

  describe "PUT #update" do
    before { valid_session }

    context "with valid attributes" do
      it "saves valid profile" do
        expect do
          put :update, { id: profile.id, profile: { fname: "Cena" } }
        end.to change{ profile.reload.fname }.to("Cena")
      end
    end

spec/factories/profiles.rb

FactoryGirl.define do
  factory :profile do
    user 
    fname "John"
    lname "Doe"
    avatar "my_avatar"
  end
end

app/controller/profiles_controller.rb

class ProfilesController < ApplicationController

private

  def user_params
    params.require(:user).permit(:id, :login, :email,
      profile_attributes: [
        :id, :user_id, :fname, :lname, :avatar, :avatar_cache
      ])
  end

end

这是运行时出现的错误rspec spec/controllers/accounts_controller_spec.rb

Failures:

  1) AccountsController PUT #update with valid attributes saves valid profile
     Failure/Error: put :update, {id: profile.id, user_id: user.id, profile: { fname: "Cena" }}
     ActionController::ParameterMissing:
       param is missing or the value is empty: user

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 rspec


    【解决方案1】:
      let(:user) { FactoryGirl.create(:user) }
      let(:profile) { FactoryGirl.create(:profile, user_id: user.id ) }
    
      describe "PUT #update" do
        before { valid_session }
    
        context "with valid attributes" do
          it "saves valid profile" do
            expect do
              put :update, id: user.id, user: { profile_attributes: { user_id: user.id, fname: "Cena" } }
            end.to change{ profile.reload.fname }.to("Cena")
          end
        end
      end
    

    【讨论】:

    • 仍然无法正常工作.. 然后我在使用您的解决方案时遇到另一个错误:NoMethodError: undefined method "permit"
    • 你可以试试我更新的答案,看看是否有效?
    • 我收到了这个错误"id" is not available from within an example (e.g. an "it" block) or from constructs that run in the scope of an example (e.g. "before", "let", etc). It is only available on an example group (e.g. a "describe" or "context" block).
    • 收到此错误expected result to have changed to "Cena", but did not change
    【解决方案2】:

    您发布的profiles_controller.rb 代码缺少更新操作(类名也是AccountController),但我猜您正在执行user.update(user_params) 之类的操作。

    如果是这种情况,正如错误所说,从控制器规范传递的参数没有:user 键,这就是导致错误的原因。

    从#user_params 方法和错误假设,控制器规范中传递给帖子的参数需要如下所示:

    { 
      user: {
        id: xxx, ...,
        profile_attributes: {
          id: xxx, 
          fname: xxx, ...
        }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多