首页 文章

put:在rspec中更新不更新属性

提问于
浏览
0

我想在我的应用程序中测试一个设计模型的 update action .

我的 factories.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

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

我希望测试通过 . 但他们失败了 . 失败的消息如下所示:

失败/错误:它{expect(@ student.first_name) . 到eql“New”}期望:“新”
得到:“约翰”

(使用eql进行比较?)
失败/错误:它{expect(@ student.last_name) . 到eql“Name”}期望:“名称”
得到:“Doe”

(使用eql进行比较?)

所以,基本上,属性没有得到更新 . 我需要做些什么才能使rspec更新属性并使测试变为绿色?

1 回答

  • 0

    我忘了在@updated_attribute哈希中包含:current_password属性 . 设计要求您在编辑表单中输入当前密码 . 所以@updated_attribute应该是,

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

相关问题