ruby rspec测试不起作用

时间:2013-03-25 04:20:38

标签: ruby rspec

我正在关注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

1 个答案:

答案 0 :(得分:1)

很明显,您要为Book类定义一个相应的构造函数,以便用三个args调用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