简体   繁体   English

把:update在rspec中不更新属性

[英]put :update in rspec not updating attributes

I am trying to test the update action of a devise Model in my app. 我正在尝试在我的应用中测试设计模型的更新操作

My factories.rb file: 我的factory.rb文件:

FactoryGirl.define do
  factory :student do
    first_name "John"
    last_name "Doe"
    sequence(:email) { |n| "#{first_name}.#{last_name}#{n}@example.com".downcase }
    city "Dhaka"
    area "Mirpur"
    zip 1216
    full_address "Mirpur, Dhaka"
    password "password"
    password_confirmation "password"
    confirmed_at Date.today
  end
end

The rspec file: rspec文件:

require 'rails_helper'

RSpec.describe Students::RegistrationsController do
  context "Student logged in" do
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:student]
      @student = FactoryGirl.create(:student)
      @updated_attributes = { :first_name => "New", :last_name => "Name" }
      sign_in @student
      put :update, :id => @student.id, :student => @updated_attributes

      @student.reload
    end

    it { expect(@student.first_name).to eql "New" }
    it { expect(@student.last_name).to eql "Name" }
  end
end

I expected the tests to pass. 我希望测试能够通过。 But they are failing. 但是他们失败了。 The failing messages look like this: 失败的消息如下所示:

Failure/Error: it { expect(@student.first_name).to eql "New" } 失败/错误:{期望(@ student.first_name).to eql“ New”}

  expected: "New" got: "John" (compared using eql?) 

Failure/Error: it { expect(@student.last_name).to eql "Name" } 失败/错误:{期望(@ student.last_name).to eql“名称”}

  expected: "Name" got: "Doe" (compared using eql?) 

So, basically, the attributes are not getting updated. 因此,基本上,属性不会更新。 What do i need to do to make rspec update the attributes and make the tests green? 我需要做些什么来使rspec更新属性并使测试变为绿色?

I forgot to include the :current_password attribute in the @updated_attribute hash. 我忘了在@updated_attribute哈希中包含:current_password属性。 Devise requires you to enter your current password in the edit form. Devise要求您在编辑表单中输入当前密码。 So, @updated_attribute should be, 因此, @ updated_attribute应该是

@updated_attributes = FactoryGirl.attributes_for(:student, :first_name => "New", :last_name => "Name", :current_password => "password" )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM