Refactoring: Migrate ticket_priority_test.rb to RSpec

This commit is contained in:
Ryan Lue 2019-01-21 16:38:45 +08:00 committed by Thorsten Eckel
parent 1694094ea5
commit 14614244cb
3 changed files with 44 additions and 60 deletions

View file

@ -0,0 +1,10 @@
FactoryBot.define do
factory :'ticket/priority', aliases: %i[ticket_priority] do
sequence :name do |n|
"#{n} urgent"
end
updated_by_id { 1 }
created_by_id { 1 }
end
end

View file

@ -0,0 +1,34 @@
require 'rails_helper'
RSpec.describe Ticket::Priority, type: :model do
describe 'Default state' do
describe 'of whole table:' do
it 'has exactly one default record' do
expect(Ticket::Priority.where(default_create: true)).to be_one
end
end
end
describe 'attributes' do
describe '#default_create' do
it 'cannot be true for more than one record at a time' do
expect { create(:'ticket/priority', default_create: true) }
.to change { Ticket::Priority.find_by(default_create: true).id }
.and change { Ticket::Priority.where(default_create: true).count }.by(0)
end
it 'cannot be false for all records' do
create(:'ticket/priority', default_create: true)
expect { Ticket::Priority.find_by(default_create: true).destroy }
.to change { Ticket::Priority.find_by(default_create: true).id }
.and change { Ticket::Priority.where(default_create: true).count }.by(0)
end
it 'is not automatically set to the last-created record' do
expect { create(:'ticket/priority') }
.not_to change { Ticket::Priority.find_by(default_create: true).id }
end
end
end
end

View file

@ -1,60 +0,0 @@
require 'test_helper'
class TicketPriorityTest < ActiveSupport::TestCase
test 'base' do
# check current state
assert_equal(1, Ticket::Priority.where(default_create: true).count)
priority_create = Ticket::Priority.find_by(default_create: true)
# add new state
priority_new2 = Ticket::Priority.create_if_not_exists(
name: 'priority 2',
updated_by_id: 1,
created_by_id: 1,
)
# verify states
assert_equal(1, Ticket::Priority.where(default_create: true).count)
assert_equal(priority_create.id, Ticket::Priority.find_by(default_create: true).id)
# cleanup
priority_new2.destroy
# verify states
assert_equal(1, Ticket::Priority.where(default_create: true).count)
assert_equal(priority_create.id, Ticket::Priority.find_by(default_create: true).id)
# add new state
priority_new3 = Ticket::Priority.create_if_not_exists(
name: 'priority 3',
default_create: true,
updated_by_id: 1,
created_by_id: 1,
)
# verify states
assert_equal(1, Ticket::Priority.where(default_create: true).count)
assert_equal(priority_new3.id, Ticket::Priority.find_by(default_create: true).id)
assert_not_equal(priority_create.id, Ticket::Priority.find_by(default_create: true).id)
# cleanup
priority_new3.destroy
# verify states
assert_equal(1, Ticket::Priority.where(default_create: true).count)
assert_equal(Ticket::Priority.first, Ticket::Priority.find_by(default_create: true))
# cleanup
priority_create.reload
priority_create.default_create = true
priority_create.save!
# verify states
assert_equal(1, Ticket::Priority.where(default_create: true).count)
assert_equal(priority_create.id, Ticket::Priority.find_by(default_create: true).id)
end
end