【问题标题】:Testing methods that get and set attributes in Rspec在 Rspec 中获取和设置属性的测试方法
【发布时间】:2015-01-17 22:11:26
【问题描述】:

这是来自rspec website 的唯一代码示例:

# bowling_spec.rb
require 'bowling'

describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    20.times { bowling.hit(0) }
    bowling.score.should eq(0)
  end
end

如果项目的要求是对每个方法都进行测试,#hit 的测试应该是什么样的?

我可以看到这个测试(一旦添加了多个示例)将同时测试#hit#score,但有没有更好的方法来隔离它们?还是说一个测试同时测试两种方法是正确的方法?

【问题讨论】:

    标签: ruby unit-testing rspec tdd


    【解决方案1】:

    如果项目的要求是对每个方法都进行测试

    所以你有一个官僚问题,而不是技术问题。如果一种方法足够简单 (hit) 可以完全指定为指定另一种方法 (score) 的副作用,那么重做规范只是为了专注于设置器是多余的。但是如果你必须这样做,你可以复制它并调整现有的描述,例如:

    describe Bowling, "#hit" do
      it "sets score to 0 for all gutter game" do
        bowling = Bowling.new
        20.times { bowling.hit(0) }
        bowling.score.should eq(0)
      end
    end
    

    ...这对我来说似乎很笨拙。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多