为什么在此rspec测试中评估此属性之前会丢失此属性?

时间:2016-12-08 15:27:03

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

我正在使用设计并覆盖了注册控制器,以允许用户被软删除"按照此处的说明操作:devise/wiki/How-to:-Soft-delete-a-user

我在代码中放了一些puts命令来跟踪测试,我的rspec结果如下所示:

    10:00:30 - INFO - Running: spec/controllers/users/registrations_controller_spec.rb
    ..
>   BEFORE: id:2, deleted_at:
>    -- model: id:2, deleted_at:2016-12-08 15:00:35 UTC
>    -- controller: id:2, deleted_at:2016-12-08 15:00:35 UTC
>   AFTER: id:2, deleted_at:
    F..

    Failures:

      1) Users::RegistrationsController GET #destroy sets 'deleted_at' date for user
         Failure/Error: expect(user.deleted_at).to_not be_nil

           expected: not nil
                got: nil
         # ./spec/controllers/users/registrations_controller_spec.rb:44:in `block (3 levels) in <top (required)>'

    Finished in 0.37438 seconds (files took 3.7 seconds to load)
    5 examples, 1 failure

puts显示时,我的用户开始将deleted_at字段设置为null。模型将deleted_at设置为当前时间,控制器确认日期已设置,但是当我在测试expect()中的user.deleted_at语句返回{{1}时}}。这似乎是我测试的问题,因为应用程序正常运行。

/spec/controllers/users/registrations_controller_spec.rb

nil

/spec/factories/user.rb

  require 'rails_helper'
  require 'factory_girl_rails'

  RSpec.describe Users::RegistrationsController, type: :controller do    
     include Devise::Test::ControllerHelpers

     describe "GET #destroy" do

        it "sets 'deleted_at' date for user" do
          request.env["devise.mapping"] = Devise.mappings[:user]

          # sign-in user
          user = FactoryGirl.create(:user_for_account_update)
          sign_in(user, scope: :user) # note: does not verify correct password

          # soft-delete user
          puts "BEFORE: id:#{user.id}, deleted_at:#{user.deleted_at}"
          delete :destroy, {:id => user.id}
          puts "AFTER: id:#{user.id}, deleted_at:#{user.deleted_at}"

          expect(user.deleted_at).to_not be_nil
        end
     end
  end

/app/models/user.rb

require 'ffaker'

FactoryGirl.define do
  factory :user do |u|
    @pass = "password"

    name FFaker::Name.name
    email { |me| "#{name.to_s.gsub(/\s/,'.')}_#{rand(1000).to_s}@testing.com" }
    password @pass

    factory :user_for_account_update do
      password_confirmation @pass
      confirmed_at Date.today
    end
  end
end

/app/controllers/users/registrations_controller.rb

  ...

  # instead of deleting, indicate the user requested a delete & timestamp it
  def soft_delete
    update_attribute(:deleted_at, Time.current)
    puts " -- model: id:#{id}, deleted_at:#{deleted_at.to_s}"
  end

  ...

1 个答案:

答案 0 :(得分:1)

您正在使用在控制器操作之前创建的user,因此它已缓存了值(包括deleted_at)。

您可以使用user.reload从DB获取更新的属性