这是我找到的一个关于Rspec的简短教程:w3ii.com: rspec,它讲解了rspec helpers。以下是示例代码:
class Dog
attr_reader :good_dog, :has_been_walked
def initialize(good_or_not)
@good_dog = good_or_not
@has_been_walked = false
end
def walk_dog
@has_been_walked = true
end
end
describe Dog do
def create_and_walk_dog(good_or_bad)
Dog.new(good_or_bad).walk_dog
end
it 'should be able to create and walk a good dog' do
dog = create_and_walk_dog(true)
expect(dog.good_dog).to be true
expect(dog.has_been_walked).to be true
end
end
当我运行这个程序时,出现了以下错误:
``` NoMethodError: undefined method 'good_dog' for true:TrueClass # ./dor.rb:22:in 'block <2 levels> in >' ```
我无法理解为什么调用 Dog.new() 方法会返回一个 true:TrueClass 对象而不是一个普通的狗。