From 14614244cbce29cd52371f68b58085569d5bf2ea Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Mon, 21 Jan 2019 16:38:45 +0800 Subject: [PATCH] Refactoring: Migrate ticket_priority_test.rb to RSpec --- spec/factories/ticket/priority.rb | 10 +++++ spec/models/ticket/priority_spec.rb | 34 ++++++++++++++++ test/unit/ticket_priority_test.rb | 60 ----------------------------- 3 files changed, 44 insertions(+), 60 deletions(-) create mode 100644 spec/factories/ticket/priority.rb create mode 100644 spec/models/ticket/priority_spec.rb delete mode 100644 test/unit/ticket_priority_test.rb diff --git a/spec/factories/ticket/priority.rb b/spec/factories/ticket/priority.rb new file mode 100644 index 000000000..ea392e38c --- /dev/null +++ b/spec/factories/ticket/priority.rb @@ -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 diff --git a/spec/models/ticket/priority_spec.rb b/spec/models/ticket/priority_spec.rb new file mode 100644 index 000000000..d662ce4dc --- /dev/null +++ b/spec/models/ticket/priority_spec.rb @@ -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 diff --git a/test/unit/ticket_priority_test.rb b/test/unit/ticket_priority_test.rb deleted file mode 100644 index 277b2d987..000000000 --- a/test/unit/ticket_priority_test.rb +++ /dev/null @@ -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