我正在尝试创建一个具有 1 个属性的 Book 类:title,如果以小写形式输入,则必须大写。我的代码在 repl.it 中工作,但 rspec 仍然显示 NoMethodError (undefined method 'title' for #(Book.... @title="Inferno") 我的代码:
class Book
def initialize(title=nil)
@title = title
end
def title=(new_title)
title = new_title.capitalize!
end
end
规格:
require 'book'
describe Book do
before do
@book = Book.new
end
describe 'title' do
it 'should capitalize the first letter' do
@book.title = "inferno"
@book.title.should == "Inferno"
end
谢谢你。