Rspec 日志规范中参数数量错误(给定 1,预期 2)的错误

Rspec error of wrong number of arguments (given 1, expected 2) in logs specs

我想测试日志方法,但我不知道为什么会出错wrong number of arguments (given 1, expected 2)

我要测试的

class:

class LogAdminData
  def initialize(admin_obj:, type:, old_data:, new_data:)
    @type = type
    @old_data = old_data
    @new_data = new_data.except(%w[created_at updated_at])
    @admin_email = admin_obj.email
    @admin_role = admin_obj.role
  end

  def call
    log_admin_data!
  end

  private

  attr_reader :type, :old_data, :new_data, :admin_email, :admin_role

  def log_admin_data!
    AdminPanelLog.update(
      admin_email: admin_email,
      admin_role: admin_role,
      type: type,
      new_data: new_data,
      old_data: old_data,
    )
  end
end

这些是规格:

RSpec.describe LogAdminData do
  include_context 'with admin_user form'

  let(:type) { 'Update attributes' }
  let!(:admin_user) { create :admin_user, :super_admin }

  describe '.call' do
    subject(:admin_logs) do
      described_class.new(
        admin_obj: admin_user,
        type: type,
        old_data: admin_user_form,
        new_data: admin_user_form,
      ).call
    end

    it { is_expected.to be_successful }
  end
end

我认为问题出在调用方法中,所以我更改了 log_admin_data! 并传递了 attr_reader 的所有参数,但这不是问题所在。

您必须更改 AdminPanelLog.updateAdminPanelLog.create 的调用,因为您创建了新记录而不是更新现有记录。

因为你有 type 列,它是为 ActiveRecord 单一 Table 继承保留的,你应该 "switch off" STI 为它设置另一个列:

class AdminPanelLog < ApplicationRecord
  self.inheritance_column = :we_dont_use_sti
end