diff --git a/spec/factories/captain/unit.rb b/spec/factories/captain/unit.rb index 64f82f710..be15c1b12 100644 --- a/spec/factories/captain/unit.rb +++ b/spec/factories/captain/unit.rb @@ -1,9 +1,12 @@ FactoryBot.define do factory :captain_unit, class: 'Captain::Unit' do association :account - association :brand, factory: :captain_brand, account: account sequence(:name) { |n| "Unidade #{n}" } inter_pix_key { SecureRandom.uuid } inter_account_number { Faker::Number.number(digits: 8) } + + brand do + Captain::Brand.find_by(account_id: account.id) || association(:captain_brand, account: account) + end end end diff --git a/spec/factories/captain_unit_factory_spec.rb b/spec/factories/captain_unit_factory_spec.rb new file mode 100644 index 000000000..ad99331d6 --- /dev/null +++ b/spec/factories/captain_unit_factory_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'captain_unit factory' do # rubocop:disable RSpec/DescribeClass + it 'cria uma unit com conta e brand prĂ³prios' do + unit = create(:captain_unit) + expect(unit).to be_persisted + expect(unit.account).to be_present + expect(unit.brand).to be_present + expect(unit.brand.account_id).to eq(unit.account_id) + end + + it 'usa a conta fornecida' do + account = create(:account) + unit = create(:captain_unit, account: account) + expect(unit.account).to eq(account) + expect(unit.brand.account_id).to eq(account.id) + end + + it 'reusa um brand existente da mesma conta' do + account = create(:account) + brand = create(:captain_brand, account: account) + unit = create(:captain_unit, account: account) + expect(unit.brand).to eq(brand) + end +end