这是我的规格和课程的代码:
describe Game do
before(:each) do
@game = Factory.build(:game)
end
describe '#no_books?' do
it 'should return true if books attribute is empty' do
@game.stub(:books).and_return([])
@game.no_books?.should be_true
end
it 'should return false if books attribute is present' do
@game.no_books?.should be_false
end
end
end
class Game
attr_reader :books
def initialize
@books = parse_books
end
def no_books?
@books.empty?
end
protected
def parse_books
# return books
end
end
然后我收到一条友好的规范失败消息:
Game#no_books? should return true if books attribute is empty
Failure/Error: @game.no_books?.should be_true
expected false to be true
就好像在使用值初始化属性书之前调用了该方法。有人可以向我解释这里发生了什么吗?