create_service_spec.rb 2.1 KB
Newer Older
1 2
require 'spec_helper'

D
Douwe Maan 已提交
3
describe Issues::CreateService, services: true do
4 5 6 7
  let(:project) { create(:empty_project) }
  let(:user) { create(:user) }

  describe :execute do
8 9
    let(:issue) { Issues::CreateService.new(project, user, opts).execute }

10
    context 'valid params' do
11 12 13 14
      let(:assignee) { create(:user) }
      let(:milestone) { create(:milestone, project: project) }
      let(:labels) { create_list(:label, 4, project: project) }

15 16
      before do
        project.team << [user, :master]
17
        project.team << [assignee, :master]
18
      end
19

20 21
      let(:opts) do
        { title: 'Awesome issue',
22
          description: 'please fix',
23 24 25
          assignee: assignee,
          label_ids: labels.map(&:id),
          milestone_id: milestone.id }
26 27
      end

28 29 30
      it { expect(issue).to be_valid }
      it { expect(issue.title).to eq('Awesome issue') }
      it { expect(issue.assignee).to eq assignee }
31 32
      it { expect(issue.labels).to match_array labels }
      it { expect(issue.milestone).to eq milestone }
33

34
      it 'creates a pending todo for new assignee' do
35 36 37 38
        attributes = {
          project: project,
          author: user,
          user: assignee,
39 40
          target_id: issue.id,
          target_type: issue.class.name,
41
          action: Todo::ASSIGNED,
42 43 44
          state: :pending
        }

45
        expect(Todo.where(attributes).count).to eq 1
46
      end
47 48 49

      context 'label that belongs to different project' do
        let(:label) { create(:label) }
50

51 52 53 54 55 56 57 58 59 60 61 62 63
        let(:opts) do
          { title: 'Title',
            description: 'Description',
            label_ids: [label.id] }
        end

        it 'does not assign label'do
          expect(issue.labels).to_not include label
        end
      end

      context 'milestone that belongs to different project' do
        let(:milestone) { create(:milestone) }
64

65 66 67 68 69 70
        let(:opts) do
          { title: 'Title',
            description: 'Description',
            milestone_id: milestone.id }
        end

71
        it 'does not assign milestone' do
72 73 74
          expect(issue.milestone).to_not eq milestone
        end
      end
75 76 77
    end
  end
end