我正在尝试 RubyMonk 测试以通过https://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/50-debugging/lessons/119-the-debugging-primaries
描述说:
现在,让我们看一个稍微复杂的练习。我已经实现了 DrivingLicenseAuthority 类,它决定是否向申请人授予许可证。但不幸的是,它是越野车。我不会告诉你所有的细节,所以你必须找出哪里出了问题以及为什么 - 测试非常乏味。目标是让所有测试都通过。记住,p 是你的朋友。
提示说
You may want to inspect all the parameters that are being passed to DrivingLicenseAuthority
You may want to ensure that the parameters are being assigned to instance variables correctly.
You may want to inspect the parameters being passed into VisualAcuity, and their types.
You may want to make sure the conditions for passing the driving test are complete.
You may want to make sure the incoming values are converted to the appropriate types before doing numeric operations that might be a float
我试过这个解决了 3 个测试中的 2 个。
class VisualAcuity
def initialize(subject, normal)
@subject = subject.to_f
@normal = normal.to_f
puts "subject: #{@subject}, normal: #{@normal}"
end
def can_drive?
(@subject / @normal) >= 0.5
end
def to_s
"VisualAcuity: subject = #{@subject}, normal = #{@normal}"
end
end
p VisualAcuity
class DrivingLicenseAuthority
def initialize(name, age, visual_acuity)
@name = name
@age = age
@visual_acuity = visual_acuity
puts "name: #{@name}, age: #{@age}, visual_acuity: #{@visual_acuity}"
end
def valid_for_license?
@age \>= 18
end
def verdict
if valid_for_license?
"#{@name} can be granted driving license"
else
"#{@name} cannot be granted driving license"
end
end
end
标准输出: 主题:20.0,正常:20.0 姓名:Ichika,年龄:20,visual_acuity:VisualAcuity:受试者= 20.0,正常= 20.0 主题:20.0,正常:200.0 姓名:Maita,年龄:20,visual_acuity:VisualAcuity:受试者 = 20.0,正常 = 200.0 主题:20.0,正常:30.0 姓名:Muluc,年龄:20,visual_acuity:VisualAcuity:受试者 = 20.0,正常 = 30.0
问题是 Maita 的视力不好,因为 subject/normal = 0.10,不满足 >=0.50
Maita 视力不好,不应该被允许开车 预期“Maita cannot be granted driving license”得到“Maita can be granted driving license”(比较使用==)
can_drive? Maita 正在通过测试 非常感谢您的帮助
def valid_for_license? @age >= 18 && @visual_acuity.can_drive? end
解决了 Maita 的测试