简体   繁体   中英

Rspec refactoring model issue (from rails test prescriptions 4)

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

Here is the Project model:

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

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

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- ie 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

Thanks so much!

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) }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM