Skip to content

Commit

Permalink
feat: finish unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGordon1 committed Jun 27, 2019
1 parent ca0ccc0 commit 388ffe7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ AllCops:
- 'script/**/*'
- 'support/**/*'
- 'tmp/**/*'
- 'test/**/*'

Metrics/BlockLength:
Enabled: true
Exclude:
- spec/**/*
ConditionalAssignment:
Enabled: false
StringLiterals:
Expand Down
11 changes: 11 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
class Task < ApplicationRecord
validates :action, presence: true, uniqueness: true
after_validation :update_finished_at, if: :will_save_change_to_done?
# only when finished_at attribute is nil
def mark_as_done
finished_at.nil? ? self.done = true : errors.add(:done, 'This task has already been marked as done')
end

private

def update_finished_at
self.finished_at = DateTime.now
end
end
30 changes: 30 additions & 0 deletions spec/models/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,34 @@

expect(task).not_to be_valid
end
describe "#mark_as_done" do
it "marks a task as done" do
task = Task.new(valid_attributes)
task.mark_as_done
expect(task.done).to eq(true)
end
it "raises an error message when called more than once" do
task = Task.new(valid_attributes)
task.mark_as_done
task.save!
expect(task.mark_as_done[0]).to eq("This task has already been marked as done")
end
end
describe '#update_finished_at' do
it "sets the finished_at attribute to a Date after the task has been marked" do
task = Task.new(valid_attributes)
task.mark_as_done
task.save
expect(task.finished_at.class).to eq(ActiveSupport::TimeWithZone)
end
it "is a private method" do
task = Task.new(valid_attributes)
expect(task.private_methods).to include(:update_finished_at)
end
it "can update the finished_at attribute of a task when called" do
task = Task.new(valid_attributes)
task.send(:update_finished_at)
expect(task.finished_at.class).to eq(ActiveSupport::TimeWithZone)
end
end
end

0 comments on commit 388ffe7

Please sign in to comment.