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

时间:2016-09-03 11:03:33

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

我正在尝试在我的应用中测试设计模型的更新操作

我的 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).to eql“New”}

   expected: "New"
        got: "John"

   (compared using eql?)
     

失败/错误:它{expect(@ student.last_name).to eql“Name”}

   expected: "Name"
        got: "Doe"

   (compared using eql?)

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

1 个答案:

答案 0 :(得分:0)

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

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