30

I'm trying to follow a guide on code.tuts and I keep getting an error.

Here is my Library spec:

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

I'm getting that have is undefined. I've figured out it's my lines

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

Is my syntax out of date? I've googled and I can't find it.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
MikaAK
  • 2,334
  • 6
  • 26
  • 53
  • 2
    Rspec 3? Perhaps there is no [such matchers at now](https://relishapp.com/rspec/rspec-expectations/v/3-0/docs/built-in-matchers/all-matcher). Use size eq – zishe Jun 08 '14 at 21:57

1 Answers1

46

The have/have_exactly, have_at_least and have_at_most matchers were removed from RSpec 3. They're now in the separate rspec-collection_matchers gem.

Or, as zishe says, instead of installing the gem, you can just use eq instead of have/have_exactly, and be >= instead of have_at_least and be <= instead of have_at_most.

Source: https://rspec.info/blog/2014/05/notable-changes-in-rspec-3/

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121