Rspec 重构模型问题(来自 Rails 测试处方 4) [英] Rspec refactoring model issue (from rails test prescriptions 4)
问题描述
我正在使用 Rails 4 处方,我有一个重构问题.我正在创建一个项目管理应用程序,我正在构建该应用程序以学习 TDD.我遇到的问题是某个特定的测试中断了,我无法弄清楚为什么会这样.这是现在可以工作的任务模型,但当我将其切换到下面的新模型时会中断:
I am going through rails 4 prescriptions and I have a refactoring question. I am creating a project management app that I am building to learn TDD.The problem I am having is that a particular test breaks and I can't figure out why it does. Here is the task model which works now but breaks when I switch it to the new one below:
class Task
attr_accessor :size, :completed_at
def initialize(options = {})
@completed = options[:completed]
@size = options[:size]
end
def mark_completed
@completed = true
end
def complete?
@completed
end
end
这是项目模型:
class Project
attr_accessor :tasks
def initialize
@tasks = []
end
def incomplete_tasks
tasks.reject(&:complete?)
end
def done?
incomplete_tasks.empty?
end
def total_size
tasks.sum(&:size)
end
def remaining_size
incomplete_tasks.sum(&:size)
end
end
rspec 测试如下所示:
The rspec test looks like this:
require 'rails_helper'
RSpec.describe Project do
describe "initialization" do
let(:project) { Project.new }
let(:task) { Task.new }
it "considers a project with no test to be done" do
expect(project).to be_done
end
it "knows that a project with an incomplete test is not done" do
project.tasks << task
expect(project).not_to be_done
end
it "marks a project done if its tasks are done" do
project.tasks << task
task.mark_completed
expect(project).to be_done
end
end
#
describe "estimates" do
let(:project) { Project.new }
let(:done) { Task.new(size: 2, completed: true) }
let(:small_not_done) { Task.new(size: 1) }
let(:large_not_done) { Task.new(size: 4) }
before(:example) do
project.tasks = [done, small_not_done, large_not_done]
end
it "can calculate total size" do
expect(project.total_size).to eq(7)
end
it "can calculate remaining size" do
expect(project.remaining_size).to eq(5)
end
end
#
end
当我在那里运行 rspec 时,它运行良好.当我重构任务模型以包含一些新功能时,我得到最后一个 rspec 失败 - 即它认为还有 7 个剩余测试而不是 5 个之前通过的测试.这是新的任务模型.
When I run the rspec there it works great. When I refactor the the task model to incorporate some new features, I get the last rspec failing- i.e. it thinks that there are 7 remaining tests instead of 5 which passed before. This is the new task model.
class Task
attr_accessor :size, :completed_at
def initialize(options = {})
mark_completed(options[:completed_at]) if options[:completed_at]
@size = options[:size]
end
def mark_completed(date = nil)
@completed_at = (date || Time.current)
end
def complete?
completed_at.present?
end
def part_of_velocity?
return false unless complete?
completed_at > 3.weeks.ago
end
def points_toward_velocity
if part_of_velocity? then size else 0 end
end
end
非常感谢!
推荐答案
对不起大家,问题最终是书注释中的一个错字.问题是我试图指定使用以下方法完成任务:
Sorry everyone, the problem ended up being a typo in the book notes. The problem was that I was trying to specify that tasks were done using:
let(:done) { Task.new(size: 2, completed: true) }
在重构任务时,代码更改为completed_at,因此我需要指定如下内容:
When in the refactoring of the Task the code changed to completed_at and thus I needed to specify something like this:
let(:old_done) { Task.new(size: 2, completed_at: 6.months.ago) }
我原以为模型中有错误,但绝对是代码.
I had thought there was a bug in the model but it was definitely the code.
这篇关于Rspec 重构模型问题(来自 Rails 测试处方 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!