测试驱动的 Rails 开发实践
在 Rails 开发中,测试驱动开发(TDD)是一种非常有效的开发方式。下面将详细介绍如何通过 TDD 来开发一个创建项目及任务的功能。
1. 模型定义与迁移
首先,定义 Task 模型,它属于一个 Project 。代码如下:
# test_first/01/app/models/task.rb
class Task < ApplicationRecord
belongs_to :project
def mark_completed(date = Time.current)
self.completed_at = date
end
def complete?
completed_at.present?
end
def part_of_velocity?
return false unless complete?
completed_at > Project.velocity_length_in_days.days.ago
end
def points_toward_velocity
part_of_velocity? ? size : 0
end
end
运行新的迁移命令:
% rake db:migrate
此时运行测试,除了新的集成测试外,其他测试应该都能通过,但可能会看到 RS
超级会员免费看
订阅专栏 解锁全文
29

被折叠的 条评论
为什么被折叠?



