snippet_spec.rb 1.9 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: snippets
#
Valery Sizov's avatar
Valery Sizov committed
5 6 7 8 9 10 11 12 13 14
#  id               :integer          not null, primary key
#  title            :string(255)
#  content          :text
#  author_id        :integer          not null
#  project_id       :integer
#  created_at       :datetime
#  updated_at       :datetime
#  file_name        :string(255)
#  type             :string(255)
#  visibility_level :integer          default(0), not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15 16
#

gitlabhq's avatar
gitlabhq committed
17 18
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
19
describe Snippet, models: true do
20 21 22 23 24 25 26 27 28 29 30
  describe 'modules' do
    subject { described_class }

    it { is_expected.to include_module(Gitlab::VisibilityLevel) }
    it { is_expected.to include_module(Linguist::BlobHelper) }
    it { is_expected.to include_module(Participable) }
    it { is_expected.to include_module(Referable) }
    it { is_expected.to include_module(Sortable) }
  end

  describe 'associations' do
31
    it { is_expected.to belong_to(:author).class_name('User') }
32
    it { is_expected.to belong_to(:project) }
33
    it { is_expected.to have_many(:notes).dependent(:destroy) }
gitlabhq's avatar
gitlabhq committed
34 35
  end

36
  describe 'validation' do
37
    it { is_expected.to validate_presence_of(:author) }
38

39
    it { is_expected.to validate_presence_of(:title) }
40
    it { is_expected.to validate_length_of(:title).is_within(0..255) }
41

42
    it { is_expected.to validate_length_of(:file_name).is_within(0..255) }
43

44
    it { is_expected.to validate_presence_of(:content) }
45 46

    it { is_expected.to validate_inclusion_of(:visibility_level).in_array(Gitlab::VisibilityLevel.values) }
gitlabhq's avatar
gitlabhq committed
47
  end
48 49 50 51 52 53 54 55 56 57 58 59 60 61

  describe '#to_reference' do
    let(:project) { create(:empty_project) }
    let(:snippet) { create(:snippet, project: project) }

    it 'returns a String reference to the object' do
      expect(snippet.to_reference).to eq "$#{snippet.id}"
    end

    it 'supports a cross-project reference' do
      cross = double('project')
      expect(snippet.to_reference(cross)).to eq "#{project.to_reference}$#{snippet.id}"
    end
  end
gitlabhq's avatar
gitlabhq committed
62
end