|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +RSpec.describe Ticket, type: :model do |
| 4 | + let(:manager) {Manager.create!(name:"arslan",email:"arslan@gmail.com",password:"password")} |
| 5 | + let(:project) { Project.create!(name: "Test Project",manager:manager) } |
| 6 | + let(:qa) { Qa.create!(email: "qa@example.com", name: "QA Tester", password: "password") } |
| 7 | + |
| 8 | + it {should belong_to(:project)} |
| 9 | + it {should belong_to(:developer).optional} |
| 10 | + it {should belong_to(:qa)} |
| 11 | + it {should have_one_attached(:screenshot)} |
| 12 | + it {should validate_presence_of(:title)} |
| 13 | + it {should validate_uniqueness_of(:title).scoped_to(:project_id)} |
| 14 | + it {should validate_presence_of(:status)} |
| 15 | + it {should validate_presence_of(:qa_id)} |
| 16 | + it {should validate_presence_of(:type)} |
| 17 | + it {should validate_presence_of(:project_id)} |
| 18 | + |
| 19 | + describe 'status enums' do |
| 20 | + it 'defines correct enum values' do |
| 21 | + expect(Ticket.statuses.keys).to contain_exactly('new_ticket','started','resolved','completed') |
| 22 | + expect(Ticket.statuses.values).to contain_exactly(0,1,2,3) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + describe 'validations' do |
| 27 | + let(:ticket) do |
| 28 | + Ticket.new( |
| 29 | + title: "Bug Screenshot Test", |
| 30 | + status: :new_ticket, |
| 31 | + type: "Bug", |
| 32 | + qa: qa, |
| 33 | + project: project |
| 34 | + ) |
| 35 | + end |
| 36 | + |
| 37 | + context 'when screenshot is PNG' do |
| 38 | + it 'is valid' do |
| 39 | + file = fixture_file_upload(Rails.root.join('spec/fixtures/files/test.png'), 'image/png') |
| 40 | + ticket.screenshot.attach(file) |
| 41 | + |
| 42 | + expect(ticket).to be_valid |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context 'when screenshot is a GIF' do |
| 47 | + it 'is valid' do |
| 48 | + file = fixture_file_upload(Rails.root.join('spec/fixtures/files/test.gif'), 'image/gif') |
| 49 | + ticket.screenshot.attach(file) |
| 50 | + |
| 51 | + expect(ticket).to be_valid |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'when screenshot is not an image' do |
| 56 | + it 'is invalid' do |
| 57 | + file = fixture_file_upload(Rails.root.join('spec/fixtures/files/test.pdf'), 'application/pdf') |
| 58 | + ticket.screenshot.attach(file) |
| 59 | + |
| 60 | + expect(ticket).not_to be_valid |
| 61 | + expect(ticket.errors[:screenshot]).to include('must be a PNG or GIF image') |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context 'when screenshot is not attached' do |
| 66 | + it 'is valid (skips screenshot validation)' do |
| 67 | + expect(ticket).to be_valid |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments