2014-06-08 18 views
24

Próbuję postępować zgodnie z przewodnikiem on code.tuts i wciąż dostaję błąd.Rspec mieć (n) .items undefined metoda

Oto moja biblioteka 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 

Dostaję że have jest niezdefiniowany. Rozumiem, że to moje linie

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

Czy moja składnia jest nieaktualna? Mam googleed i nie mogę go znaleźć.

+2

Rspec 3? Być może obecnie nie ma [takich matchers] (https://relishapp.com/rspec/rspec-expectations/v/3-0/docs/built-in-matchers/all-matcher). Użyj rozmiaru eq – zishe

Odpowiedz

42

W have/have_exactly, have_at_least i have_at_most dopasowujących wyjęto z RSpec 3. Są teraz w osobnej rspec-collection_matchers gem.

Albo, jak zishe mówi, zamiast instalowania gem, można po prostu użyć eq zamiast have/have_exactly i be >= zamiast have_at_least i be <= zamiast have_at_most.

Źródło: http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3

+0

@ br3nt, RSpec 2 tworzy zbyt wiele sposobów na jedną rzecz, to jest trudne dla początkujących –

Powiązane problemy