【发布时间】:2016-03-02 05:19:44
【问题描述】:
学习 Rspec,只使用 Ruby,而不是 Rails。我有一个在命令行中按预期工作的脚本,但我无法通过测试。
相关代码:
class Tree
attr_accessor :height, :age, :apples, :alive
def initialize
@height = 2
@age = 0
@apples = false
@alive = true
end
def age!
@age += 1
end
以及规格:
describe "Tree" do
before :each do
@tree = Tree.new
end
describe "#age!" do
it "ages the tree object one year per call" do
10.times { @tree.age! }
expect(@age).to eq(10)
end
end
end
还有错误:
1) Tree #age! ages the tree object one year per call
Failure/Error: expect(@age).to eq(10)
expected: 10
got: nil
(compared using ==)
我认为这就是所有相关代码,如果我发布的代码中缺少某些内容,请告诉我。据我所知,错误来自 rspec 中的范围,并且 @age 变量没有以我认为应该的方式传递到 rspec 测试中,因此在尝试调用测试中的函数时为零。
【问题讨论】: