Refactoring: Migrate ticket_state_test to RSpec

This commit is contained in:
Ryan Lue 2019-02-14 17:44:03 +08:00 committed by Martin Edenhofer
parent 9637d1200e
commit 0ab78d9f1c
2 changed files with 180 additions and 105 deletions

View file

@ -6,17 +6,188 @@ RSpec.describe Ticket::State, type: :model do
it_behaves_like 'ApplicationModel'
it_behaves_like 'CanBeImported'
describe '.by_category' do
it 'looks up states by category' do
expect(described_class.by_category(:open))
.to be_an(ActiveRecord::Relation)
.and include(instance_of(Ticket::State))
describe 'Default state' do
describe 'of whole table:' do
it 'has seven records' do
expect(Ticket::State.pluck(:name))
.to match_array(%w[closed merged new open pending\ close pending\ reminder removed])
end
end
context 'with invalid category name' do
it 'raises RuntimeError' do
expect { described_class.by_category(:invalidcategoryname) }
.to raise_error(RuntimeError)
describe 'of "new" state:' do
it 'is the sole #default_create state' do
expect(Ticket::State.where(default_create: true))
.to match_array([Ticket::State.find_by(name: 'new')])
end
end
describe 'of "open" state:' do
it 'is the sole #default_follow_up state' do
expect(Ticket::State.where(default_follow_up: true))
.to match_array([Ticket::State.find_by(name: 'open')])
end
end
end
describe 'Class methods:' do
describe '.by_category' do
it 'looks up states by category' do
expect(described_class.by_category(:open))
.to be_an(ActiveRecord::Relation)
.and include(instance_of(Ticket::State))
end
context 'with invalid category name' do
it 'raises RuntimeError' do
expect { described_class.by_category(:invalidcategoryname) }
.to raise_error(RuntimeError)
end
end
end
end
describe 'Attributes:' do
describe '#default_create' do
let!(:original_default) { Ticket::State.find_by(default_create: true) }
context 'for newly created record' do
subject!(:state) { build(:ticket_state, default_create: default_create) }
context 'when true' do
let(:default_create) { true }
it 'unsets previous default' do
expect { state.save }
.to change { original_default.reload.default_create }.to(false)
.and not_change { Ticket::State.where(default_create: true).count }
end
end
context 'when false' do
let(:default_create) { false }
it 'does not alter existing default' do
expect { state.save }
.to not_change { Ticket::State.find_by(default_create: true) }
.and not_change { Ticket::State.where(default_create: true).count }
end
end
end
context 'for existing record' do
subject!(:state) { create(:ticket_state, default_create: default_create) }
context 'when true' do
let(:default_create) { true }
context 'and updated to false' do
it 'assigns Ticket::State.first as default' do
expect { state.update(default_create: false) }
.to change { Ticket::State.first.default_create }.to(true)
.and not_change { Ticket::State.where(default_create: true).count }
end
end
context 'and destroyed' do
it 'assigns Ticket::State.first as default' do
expect { state.destroy }
.to change { Ticket::State.first.default_create }.to(true)
.and not_change { Ticket::State.where(default_create: true).count }
end
end
end
context 'when false' do
let(:default_create) { false }
context 'and updated to true' do
it 'unsets previous default' do
expect { state.update(default_create: true) }
.to change { original_default.reload.default_create }.to(false)
.and not_change { Ticket::State.where(default_create: true).count }
end
end
context 'and destroyed' do
it 'does not alter existing default' do
expect { state.destroy }
.to not_change { Ticket::State.find_by(default_create: true) }
.and not_change { Ticket::State.where(default_create: true).count }
end
end
end
end
end
describe '#default_follow_up' do
let!(:original_default) { Ticket::State.find_by(default_follow_up: true) }
context 'for newly created record' do
subject!(:state) { build(:ticket_state, default_follow_up: default_follow_up) }
context 'when true' do
let(:default_follow_up) { true }
it 'unsets previous default' do
expect { state.save }
.to change { original_default.reload.default_follow_up }.to(false)
.and not_change { Ticket::State.where(default_follow_up: true).count }
end
end
context 'when false' do
let(:default_follow_up) { false }
it 'does not alter existing default' do
expect { state.save }
.to not_change { Ticket::State.find_by(default_follow_up: true) }
.and not_change { Ticket::State.where(default_follow_up: true).count }
end
end
end
context 'for existing record' do
subject!(:state) { create(:ticket_state, default_follow_up: default_follow_up) }
context 'when true' do
let(:default_follow_up) { true }
context 'and updated to false' do
it 'assigns Ticket::State.first as default' do
expect { state.update(default_follow_up: false) }
.to change { Ticket::State.first.default_follow_up }.to(true)
.and not_change { Ticket::State.where(default_follow_up: true).count }
end
end
context 'and destroyed' do
it 'assigns Ticket::State.first as default' do
expect { state.destroy }
.to change { Ticket::State.first.default_follow_up }.to(true)
.and not_change { Ticket::State.where(default_follow_up: true).count }
end
end
end
context 'when false' do
let(:default_follow_up) { false }
context 'and updated to true' do
it 'unsets previous default' do
expect { state.update(default_follow_up: true) }
.to change { original_default.reload.default_follow_up }.to(false)
.and not_change { Ticket::State.where(default_follow_up: true).count }
end
end
context 'and destroyed' do
it 'does not alter existing default' do
expect { state.destroy }
.to not_change { Ticket::State.find_by(default_follow_up: true) }
.and not_change { Ticket::State.where(default_follow_up: true).count }
end
end
end
end
end
end

View file

@ -1,96 +0,0 @@
require 'test_helper'
class TicketStateTest < ActiveSupport::TestCase
test 'base' do
# check current state
assert_equal(1, Ticket::State.where(default_create: true).count)
assert_equal(1, Ticket::State.where(default_follow_up: true).count)
state_create = Ticket::State.find_by(default_create: true)
state_follow_up = Ticket::State.find_by(default_follow_up: true)
# add new state
state_new2 = Ticket::State.create_if_not_exists(
name: 'new 2',
state_type_id: Ticket::StateType.find_by(name: 'new').id,
updated_by_id: 1,
created_by_id: 1,
)
state_follow_up2 = Ticket::State.create_if_not_exists(
name: 'open 2',
state_type_id: Ticket::StateType.find_by(name: 'open').id,
updated_by_id: 1,
created_by_id: 1,
)
# verify states
assert_equal(1, Ticket::State.where(default_create: true).count)
assert_equal(1, Ticket::State.where(default_follow_up: true).count)
assert_equal(state_create.id, Ticket::State.find_by(default_create: true).id)
assert_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id)
# cleanup
state_new2.destroy
state_follow_up2.destroy
# verify states
assert_equal(1, Ticket::State.where(default_create: true).count)
assert_equal(1, Ticket::State.where(default_follow_up: true).count)
assert_equal(state_create.id, Ticket::State.find_by(default_create: true).id)
assert_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id)
# add new state
state_new3 = Ticket::State.create_if_not_exists(
name: 'new 3',
state_type_id: Ticket::StateType.find_by(name: 'new').id,
default_create: true,
updated_by_id: 1,
created_by_id: 1,
)
state_follow_up3 = Ticket::State.create_if_not_exists(
name: 'open 3',
state_type_id: Ticket::StateType.find_by(name: 'open').id,
default_follow_up: true,
updated_by_id: 1,
created_by_id: 1,
)
# verify states
assert_equal(1, Ticket::State.where(default_create: true).count)
assert_equal(1, Ticket::State.where(default_follow_up: true).count)
assert_not_equal(state_create.id, Ticket::State.find_by(default_create: true).id)
assert_equal(state_new3.id, Ticket::State.find_by(default_create: true).id)
assert_not_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id)
assert_equal(state_follow_up3.id, Ticket::State.find_by(default_follow_up: true).id)
# cleanup
state_new3.destroy
state_follow_up3.destroy
# verify states
assert_equal(1, Ticket::State.where(default_create: true).count)
assert_equal(1, Ticket::State.where(default_follow_up: true).count)
assert_equal(state_create.id, Ticket::State.find_by(default_create: true).id)
assert_not_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id)
# cleanup
state_create.reload
state_create.default_create = true
state_create.save!
state_follow_up.reload
state_follow_up.default_follow_up = true
state_follow_up.save!
# verify states
assert_equal(1, Ticket::State.where(default_create: true).count)
assert_equal(1, Ticket::State.where(default_follow_up: true).count)
assert_equal(state_create.id, Ticket::State.find_by(default_create: true).id)
assert_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id)
end
end