0

我正在关注http://goo.gl/7Dlv5。视频创建了一个类

class Book

end

测试规范/book_spec.rb 看起来像:

require "spec_helper"

describe Book do

    before :each do
        @book = Book.new "Title","Author", :category
    end

    describe "#new" do
        it "returns a new book object" do
            @book.should be_an_instance_of Book
        end
    end
end

测试通过了作者。它对我来说失败了。所以我猜红宝石有什么变化?或者也许是我在代码中找不到的错字。你能帮忙吗?

这是我的结果。谢谢你。

Failures:

  1) Book#new returns a new book object
     Failure/Error: @book = Book.new "Title","Author", :category
     ArgumentError:
       wrong number of arguments(3 for 0)
     # ./spec/book_spec.rb:6:in `initialize'
     # ./spec/book_spec.rb:6:in `new'
     # ./spec/book_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.00058 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/book_spec.rb:11 # Book#new returns a new book object
4

1 回答 1

1

很明显,您将为要使用三个参数Book调用的类定义一个相应的构造函数。Book.new

上面的链接清楚地表明(看文字记录):

# These will fail, so here’s the code for Book to make them pass:

class Book
    attr_accessor :title, :author, :category
        def initialize title, author, category
            @title = title
            @author = author
            @category = category
        end
end
于 2013-03-25T04:24:56.390 回答