ruby Rspec to have(n).items undefined方法

llmtgqce  于 2023-03-22  发布在  Ruby
关注(0)|答案(1)|浏览(71)

我正在尝试遵循on code.tuts指南,但总是收到错误。
以下是我的库规范:

require 'spec_helper'

describe Library do
  before :all do
    lib_arr = [
      Book.new("JavaScript: The Good Parts", "Douglas Crockford", :development),
      Book.new("Dont Make me Think", "Steve Krug", :usability),
    ]

    File.open "books.yml", "w" do |f|
      f.write YAML::dump lib_arr
    end
  end

  before :each do
    @lib = Library.new "books.yml"
  end

  describe  "#new" do
    context "with no parameters" do
      it "has no book" do
        lib = Library.new
        expect(lib).to have(0).books
      end
    end

    context "with a yaml file name parameters" do
      it "has two books" do
        expect(@lib).to_have(0).books
      end
    end
  end

  it "returns all the books in a given category" do
    expect(@lib.get_books_in_category(:development).length).to eql 1
  end

  it "accepts new books" do
    @lib.add_book(Book.new("Designing for the Web", "Mark Boulton", :design))
    expect(@lib.get_book("Designing for the Web")).to be_an_instance_of Book
  end

  it "saves the library" do
    books = @lib.books.map { |book| book.title}
    @lib.save
    lib2 = Library.new 'books.yml'
    books2 = lib2.books.map { |book| book.title }
    expect(books).to eql books2
  end
end

我得到了have是未定义的。我已经弄清楚了这是我的行

expect(@lib).to have(0).books
expect(lib).to have(0).books

我的语法过时了吗?我用谷歌搜索了一下,但找不到。

zqry0prt

zqry0prt1#

have/have_exactlyhave_at_leasthave_at_most匹配器已从RSpec 3中删除。它们现在位于单独的rspec-collection_matchers gem中。
或者,就像zishe说的,你可以用eq代替have/have_exactly,用be >=代替have_at_least,用be <=代替have_at_most,而不用安装gem。
来源:https://rspec.info/blog/2014/05/notable-changes-in-rspec-3/

相关问题