0

I want to test logs method and I don't know why I've got an error wrong number of arguments (given 1, expected 2)

class which I want to test:

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

and those are the specs:

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

I thought the issue is in call method so I've changed log_admin_data! and passed all arguments from attr_reader but that wasn't the issue.

mr_muscle
  • 2,536
  • 18
  • 61
  • Should it be `AdminPanelLog.create` instead of `.update` ? – Pavel Mikhailyuk Sep 10 '19 at 05:58
  • If I change it to `create` I will have this error ` `ActiveRecord::SubclassNotFound:` `The single-table inheritance mechanism failed to locate the subclass: 'Update attributes'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite AdminPanelLog.inheritance_column to use another column for that information.` – mr_muscle Sep 10 '19 at 08:16
  • 1
    Read about AtiveRecord's single table inheritance. TL;DR; `type` is reserved column name. If you need it, redefine STI column name - https://stackoverflow.com/questions/20294879/change-activerecordbase-inheritance-column-in-a-rails-app – Pavel Mikhailyuk Sep 10 '19 at 08:28
  • Yes, you were right. If you want to you can add an answer that I could mark it as correct, valuable answer. – mr_muscle Sep 10 '19 at 09:40
  • Glad to help you :) – Pavel Mikhailyuk Sep 10 '19 at 10:21

1 Answers1

1

You have to change AdminPanelLog.update call on AdminPanelLog.create one because you create new record and not update existing one.

As you have type column, which is reserved for ActiveRecord Single Table Inheritance, you should "switch off" STI by setting another column for it:

class AdminPanelLog < ApplicationRecord
  self.inheritance_column = :we_dont_use_sti
end
Pavel Mikhailyuk
  • 2,757
  • 9
  • 17