From b3cb67836df67b3fbbe136c47b047df97368eb79 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Thu, 7 Feb 2019 22:46:16 +0800 Subject: [PATCH] Refactoring: Migrate email_address_test.rb to RSpec --- spec/factories/email_address.rb | 8 +-- spec/models/email_address_spec.rb | 66 +++++++++++++++++++++ test/unit/email_address_test.rb | 95 ------------------------------- 3 files changed, 70 insertions(+), 99 deletions(-) create mode 100644 spec/models/email_address_spec.rb delete mode 100644 test/unit/email_address_test.rb diff --git a/spec/factories/email_address.rb b/spec/factories/email_address.rb index 9ffd6f873..a576e7ae3 100644 --- a/spec/factories/email_address.rb +++ b/spec/factories/email_address.rb @@ -1,9 +1,9 @@ FactoryBot.define do factory :email_address do - sequence(:email) { |n| "zammad#{n}@localhost.com" } + association :channel, factory: :email_channel + sequence(:email) { |n| "zammad#{n}@localhost.com" } sequence(:realname) { |n| "zammad#{n}" } - channel { create(:email_channel) } - created_by_id 1 - updated_by_id 1 + created_by_id { 1 } + updated_by_id { 1 } end end diff --git a/spec/models/email_address_spec.rb b/spec/models/email_address_spec.rb new file mode 100644 index 000000000..d0ebdce93 --- /dev/null +++ b/spec/models/email_address_spec.rb @@ -0,0 +1,66 @@ +require 'rails_helper' + +RSpec.describe EmailAddress, type: :model do + subject(:email_address) { create(:email_address) } + + describe 'Attributes:' do + describe '#active' do + subject(:email_address) do + create(:email_address, channel: channel, active: active) + end + + context 'without a Channel association' do + let(:channel) { nil } + let(:active) { true } + + it 'always returns false' do + expect(email_address.active).not_to eq(active) + end + end + + context 'with a Channel association' do + let(:channel) { create(:email_channel) } + let(:active) { true } + + it 'returns the value it was set to' do + expect(email_address.active).to eq(active) + end + end + end + end + + describe 'Associations:' do + describe '#groups' do + let(:group) { create(:group, email_address: email_address) } + + context 'when an EmailAddress is destroyed' do + it 'removes the #email_address_id from all associated Groups' do + expect { email_address.destroy } + .to change { group.reload.email_address_id }.to(nil) + end + end + end + + describe '#channel' do + subject(:email_addresses) { create_list(:email_address, 2, channel: channel) } + let(:channel) { create(:channel) } + + context 'when a Channel is destroyed' do + it 'removes the #channel_id from all its associated EmailAddresses' do + expect { channel.destroy } + .to change { email_addresses.map(&:reload).map(&:channel_id) } + .to([nil, nil]) + end + + context 'and then an identical Channel is created' do + it 'removes the #channel_id from all its associated EmailAddresses' do + channel.destroy + + expect { create(:channel) } + .not_to change { email_addresses.map(&:reload).map(&:channel_id) } + end + end + end + end + end +end diff --git a/test/unit/email_address_test.rb b/test/unit/email_address_test.rb deleted file mode 100644 index f98778ea0..000000000 --- a/test/unit/email_address_test.rb +++ /dev/null @@ -1,95 +0,0 @@ -require 'test_helper' - -class EmailAddressTest < ActiveSupport::TestCase - test 'basic tests' do - - email_address1 = EmailAddress.create_or_update( - realname: 'address #1', - email: 'address1@example.com', - active: true, - updated_by_id: 1, - created_by_id: 1, - ) - assert_not(email_address1.active) - - email_address1.channel_id = Channel.first.id - email_address1.save - - assert(email_address1.active) - end - - test 'group tests' do - - email_address1 = EmailAddress.create_or_update( - realname: 'address #1', - email: 'address1@example.com', - active: true, - updated_by_id: 1, - created_by_id: 1, - ) - - group1 = Group.create_or_update( - name: 'group email address 1', - email_address_id: email_address1.id, - active: true, - updated_by_id: 1, - created_by_id: 1, - ) - assert(group1.email_address_id) - email_address1.destroy - - group1 = Group.find(group1.id) - assert_nil(group1.email_address_id, 'References to groups are deleted') - end - - test 'channel tests' do - - channel1 = Channel.create( - area: 'Email::Account', - options: {}, - active: true, - updated_by_id: 1, - created_by_id: 1, - ) - - email_address1 = EmailAddress.create_or_update( - realname: 'address #1', - email: 'address1@example.com', - active: true, - channel_id: channel1.id, - updated_by_id: 1, - created_by_id: 1, - ) - email_address2 = EmailAddress.create_or_update( - realname: 'address #2', - email: 'address2@example.com', - active: true, - channel_id: channel1.id, - updated_by_id: 1, - created_by_id: 1, - ) - - channel1.destroy - - email_address1 = EmailAddress.find(email_address1.id) - assert_not(email_address1.channel_id) - - email_address2 = EmailAddress.find(email_address2.id) - assert_not(email_address2.channel_id) - - channel1 = Channel.create( - area: 'Email::Account', - options: {}, - active: true, - updated_by_id: 1, - created_by_id: 1, - ) - - email_address1 = EmailAddress.find(email_address1.id) - assert_not(email_address1.channel_id) - - email_address2 = EmailAddress.find(email_address2.id) - assert_not(email_address2.channel_id) - end - -end