time_accounting_spec.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. require 'rails_helper'
  2. RSpec.describe Ticket::TimeAccounting, type: :model do
  3. subject(:time_accounting) { create(:'ticket/time_accounting') }
  4. describe 'Associations:' do
  5. describe '#ticket_article' do
  6. subject!(:time_accounting) { create(:'ticket/time_accounting', :for_article) }
  7. context 'when destroyed' do
  8. it 'destroys self' do
  9. expect { time_accounting.ticket_article.destroy }
  10. .to change(time_accounting, :persisted?).to(false)
  11. .and change(described_class, :count).by(-1)
  12. end
  13. it 'does not destroy other TimeAccountings for same ticket' do
  14. create(:'ticket/time_accounting', ticket: time_accounting.ticket)
  15. create(:'ticket/time_accounting', :for_article, ticket: time_accounting.ticket)
  16. expect { time_accounting.ticket_article.destroy }
  17. .to change(described_class, :count).by(-1)
  18. end
  19. end
  20. context 'when recalculating articles' do
  21. let(:ticket) { create(:ticket) }
  22. let(:article1) { create(:ticket_article, ticket: ticket) }
  23. let(:article2) { create(:ticket_article, ticket: ticket) }
  24. it 'one article' do
  25. time_accounting = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1)
  26. expect(ticket.reload.time_unit).to eq(time_accounting.time_unit)
  27. end
  28. it 'multiple article' do
  29. time_accounting1 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  30. time_accounting2 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  31. expect(ticket.reload.time_unit).to eq(time_accounting1.time_unit + time_accounting2.time_unit)
  32. end
  33. it 'destroy article' do
  34. time_accounting1 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  35. create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  36. article2.destroy
  37. expect(ticket.reload.time_unit).to eq(time_accounting1.time_unit)
  38. end
  39. it 'destroy all articles' do
  40. create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  41. create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  42. article1.destroy
  43. article2.destroy
  44. expect(ticket.reload.time_unit).to eq(0)
  45. end
  46. end
  47. end
  48. end
  49. end