todo_spec.rb 1.8 KB
Newer Older
D
Douglas Barbosa Alexandre 已提交
1 2
# == Schema Information
#
3
# Table name: todos
D
Douglas Barbosa Alexandre 已提交
4 5 6 7 8 9 10
#
#  id          :integer          not null, primary key
#  user_id     :integer          not null
#  project_id  :integer          not null
#  target_id   :integer          not null
#  target_type :string           not null
#  author_id   :integer
11
#  note_id     :integer
D
Douglas Barbosa Alexandre 已提交
12
#  action      :integer          not null
D
Douglas Barbosa Alexandre 已提交
13 14 15 16 17 18 19
#  state       :string           not null
#  created_at  :datetime
#  updated_at  :datetime
#

require 'spec_helper'

20
describe Todo, models: true do
D
Douglas Barbosa Alexandre 已提交
21 22
  describe 'relationships' do
    it { is_expected.to belong_to(:author).class_name("User") }
23
    it { is_expected.to belong_to(:note) }
D
Douglas Barbosa Alexandre 已提交
24 25 26 27 28
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:target).touch(true) }
    it { is_expected.to belong_to(:user) }
  end

29 30 31 32 33
  describe 'respond to' do
    it { is_expected.to respond_to(:author_name) }
    it { is_expected.to respond_to(:author_email) }
  end

D
Douglas Barbosa Alexandre 已提交
34 35 36 37 38
  describe 'validations' do
    it { is_expected.to validate_presence_of(:action) }
    it { is_expected.to validate_presence_of(:target) }
    it { is_expected.to validate_presence_of(:user) }
  end
39

40
  describe '#body' do
41
    before do
42
      subject.target = build(:issue, title: 'Bugfix')
43 44
    end

45
    it 'returns target title when note is blank' do
46 47
      subject.note = nil

48
      expect(subject.body).to eq 'Bugfix'
49 50 51 52 53
    end

    it 'returns note when note is present' do
      subject.note = build(:note, note: 'quick fix')

54
      expect(subject.body).to eq 'quick fix'
55 56 57
    end
  end

58 59 60 61
  describe '#done!' do
    it 'changes state to done' do
      todo = create(:todo, state: :pending)
      expect { todo.done! }.to change(todo, :state).from('pending').to('done')
62 63
    end

64 65 66
    it 'does not raise error when is already done' do
      todo = create(:todo, state: :done)
      expect { todo.done! }.not_to raise_error
67
    end
68
  end
D
Douglas Barbosa Alexandre 已提交
69
end