Skip to content

Commit

Permalink
add spec for accountable concern
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Dec 14, 2023
1 parent 85f4eca commit 02d5f60
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 1 deletion.
117 changes: 117 additions & 0 deletions spec/models/concerns/accountable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# frozen_string_literal: true

require 'rails_helper'
require 'spec_helper'

describe Accountable, type: :concern do
let(:account) { create(:account) }

describe '.has_account' do
let(:accountable) {
Class.new ActiveRecord::Base do
def self.table_name = 'licenses'
def self.name = 'License'

include Accountable
end
}

it 'should not raise' do
expect { accountable.has_account }.to_not raise_error
end

it 'should define an account association' do
accountable.has_account

association = accountable.reflect_on_association(:account)

expect(association).to_not be_nil
end

it 'should define an account association with options' do
accountable.has_account inverse_of: :licenses, foreign_key: :account_id

association = accountable.reflect_on_association(:account)

expect(association.options).to include(
foreign_key: :account_id,
inverse_of: :licenses,
)
end

context 'with incorrect usage' do
it 'should not warn on belongs_to defined before account' do
expect {
accountable.belongs_to :user
accountable.has_account
}.to_not log anything
end

it 'should warn on belongs_to defined after account' do
expect {
accountable.has_account
accountable.belongs_to :user
}.to log.warning <<~MSG
A .belongs_to(:user) association was defined after .has_account() was called.
MSG
end
end

context 'without default' do
before { accountable.has_account }

it 'should have a nil default' do
instance = accountable.new

expect(instance.account_id).to be_nil
expect(instance.account).to be_nil
end
end

context 'with default' do
let(:account) { create(:account) }

context 'with string' do
before {
acct = account # close over account

accountable.has_account default: -> { acct.id }
}

it 'should have an account default' do
instance = accountable.new

expect(instance.account_id).to eq account.id
expect(instance.account).to eq account
end
end

context 'with class' do
before {
acct = account # close over account

accountable.has_account default: -> { acct }
}

it 'should have an account default' do
instance = accountable.new

expect(instance.account_id).to eq account.id
expect(instance.account).to eq account
end
end

context 'with other' do
before {
accountable.has_account default: -> { Class.new }
}

it 'should have an account default' do
expect { accountable.new }.to raise_error NoMatchingPatternError
end
end
end
end

# NOTE(ezekg) See :accountable shared examples for more tests
end
2 changes: 1 addition & 1 deletion spec/models/concerns/environmental_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
describe Environmental, type: :concern do
let(:account) { create(:account) }

describe '.has_environent' do
describe '.has_environment' do
let(:environmental) {
Class.new ActiveRecord::Base do
def self.table_name = 'licenses'
Expand Down
60 changes: 60 additions & 0 deletions spec/support/matchers/log_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

RSpec::Matchers.define :log do |message|
supports_block_expectations

match do |block|
@levels ||= %i[error warn info debug]
@expected ||= message
@messages = []

@levels.each do |level|
allow(Rails.logger).to(
receive(level) { @messages << _1.strip }.and_return(nil),
)
end

block.call

expect(@messages).to be_any { |actual|
expected = case @expected
in String => s
/#{Regexp.escape(s)}/
in Regexp => re
re
end

expected.match?(actual)
}
end

chain :error do |message|
@levels = %i[error]
@expected = message
end

chain :warning do |message|
@levels = %i[warn]
@expected = message
end

chain :info do |message|
@levels = %i[info]
@expected = message
end

chain :debug do |message|
@levels = %i[debug]
@expected = message
end

failure_message do
<<~MSG
Expected block to output matching log messages to the following log levels: #{@levels.inspect}.
expected:
#{@expected.inspect}
actual:
#{@messages.join.inspect}
MSG
end
end

0 comments on commit 02d5f60

Please sign in to comment.