本文介绍了Rspec to have(n).items undefined 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循关于 code.tuts 的指南 并且我不断收到错误消息.

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

这是我的库规范:

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 未定义.我发现这是我的台词

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.

推荐答案

have/have_exactlyhave_at_leasthave_at_most 匹配器已从 RSpec 3 中删除.它们现在位于单独的 rspec-collection_matchers gem 中.

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.

或者,正如 zishe 所说,您可以使用 eq 而不是 have/have_exactly 和 be >= 而不是 have_at_leastbe <= 而不是 have_at_most.

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.

来源:http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3

这篇关于Rspec to have(n).items undefined 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:23