2015-06-10 70 views
0

運行我的RSpec測試時,我收到了以下加載錯誤:爲什麼我在RSpec測試中收到加載錯誤?

Eric-Parks-MacBook-Pro-2:Address_Bloc ericpark$ rspec spec/address_book_spec.rb models/address_book.rbrspec spec/address_book_spec.rb models/address_book.rb 
/Users/ericpark/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `load': cannot load such file -- /Users/ericpark/rails_projects/Address_Bloc/models/address_book.rbrspec (LoadError) 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `each' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `load_spec_files' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:97:in `setup' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:85:in `run' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:70:in `run' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:38:in `invoke' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/exe/rspec:4:in `<top (required)>' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `load' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `<main>' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval' 
    from /Users/ericpark/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>' 

我不知道如何和爲什麼加載出錯?下面是我的一些代碼:

address_book_spec.rb:

RSpec.describe AddressBook do #describe the specific MVC architecture 
    let(:book) { AddressBook.new } #Instantiates book as AddressBook.new removing the need to continually call it 

    def check_entry(entry, expected_name, expected_number, expected_email) 
    expect(entry.name).to eql expected_name 
    expect(entry.phone_number).to eql expected_number 
    expect(entry.email).to eql expected_email 
    end 

    context "attributes" do #RSpec paradigm to name the test 
    it "should respond to entries" do #RSpec paradigm to describe the function 
     expect(book).to respond_to(:entries) #Actual code that runs 
    end 
    it "should initialize entries as an array" do 
     expect(book.entries).to be_a(Array) 
    end 

    it "should initialize entries as empty" do 
     expect(book.entries.size).to eq 0 
    end 
    end 

    context ".add_entry" do 
    it "adds only one entry to the address book" do 
     book.add_entry('Ada Lovelace', '010.012.1815', '[email protected]') 

     expect(book.entries.size).to eq 1 
    end 

    it "adds the correct information to entries" do 
     book.add_entry('Ada Lovelace', '010.012.1815', '[email protected]') 
     new_entry = book.entries[0] 

     expect(new_entry.name).to eq 'Ada Lovelace' 
     expect(new_entry.phone_number).to eq '010.012.1815' 
     expect(new_entry.email).to eq '[email protected]' 
    end 
    end 

    context ".remove_entry" do 
    it "deletes one entry of the address book" do 
     book = AddressBook.new 
     book.add_entry('Ada Lovelace', '010.012.1815', '[email protected]') 

     book.remove_entry('[email protected]') 
     expect(book.entries.size).to eq(0) 
    end 
    end 

    context ".import_from_csv" do 
    it "imports the correct number of entries" do 
     book.import_from_csv("entries.csv") 
     book_size = book.entries.size 
     expect(book_size).to eql 5 
    end 

    it "imports the 1st entry" do 
     book.import_from_csv("entries.csv") 
     entry_one = book.entries[0] 
     check_entry(entry_one, "Bill", "555-555-5555", "[email protected]") 
    end 

    it "imports the 2nd entry" do 
     book.import_from_csv("entries.csv") 
     entry_two = book.entries[1] 
     check_entry(entry_two, "Bob", "555-555-5555", "[email protected]") 
    end 

    it "imports the 3rd entry" do 
     book.import_from_csv("entries.csv") 
     entry_three = book.entries[2] 
     check_entry(entry_three, "Joe", "555-555-5555", "[email protected]") 
    end 

    it "imports the 4th entry" do 
     book.import_from_csv("entries.csv") 
     entry_four = book.entries[3] 
     check_entry(entry_four, "Sally", "555-555-5555", "[email protected]") 
    end 

    it "imports the 5th entry" do 
     book.import_from_csv("entries.csv") 
     entry_five = book.entries[4] 
     check_entry(entry_five, "Sussie", "555-555-5555", "[email protected]") 
    end 
    end 

    context ".import_from_csv2" do 
     it "imports the correct number of entries" do 
     book.import_from_csv("entries_2.csv") 
     book_size = book.entries.size 
     expect(book_size).to eql 3 
     end 

     it "imports the 1st entry" do 
     book.import_from_csv("entries_2.csv") 
     entry_one = book.entries[0] 
     check_entry(entry_one, "Eric", "111-111-1111", "[email protected]") 
     end 

     it "imports the 2nd entry" do 
     book.import_from_csv("entries_2.csv") 
     entry_two = book.entries[1] 
     check_entry(entry_two, "Anna", "222-222-2222", "[email protected]") 
    end 

     it "imports the 3rd entry" do 
     book.import_from_csv("entries_2.csv") 
     entry_three = book.entries[2] 
     check_entry(entry_three, "Tony", "222-222-2222", "[email protected]") 
     end 
    end 
end 

address_book.rb:

require_relative "entry.rb" #Loading the model's association 
require "csv" 
class AddressBook 
    attr_accessor :entries 

    def initialize 
    @entries = [] 
    end 

    def add_entry(name, phone, email) 
    #Create a variable index to store 
    index = 0 
    @entries.each do |entry| 
     if name < entry.name 
     break 
     end 
     index += 1 
    end 

    @entries.insert(index,Entry.new(name,phone,email)) 
    end 

    def import_from_csv(file_name) 
    csv_text = File.read(file_name) 
    csv = CSV.parse(csv_text, headers: true) 
    csv.each do |row| 
     row_hash = row.to_hash 
     add_entry(row_hash["name"], row_hash["phone_number"], row_hash["email"]) 
    end 
    return csv.count 
    end 

    def binary_search(name) 
    return nil 
    end 
end 

我已經包括了entries.csv和同一文件夾內的entries_2.csv應用程序。

+1

無法加載的文件是'models/address_book.rbrspec' - 那是關於什麼的? –

回答

0

錯誤告訴你它無法加載你已經告訴它加載的特定文件。注意錯誤:

cannot load such file -- /Users/ericpark/rails_projects/Address_Bloc/models/address_book.rbrspec (LoadError) 

而且注意你的rspec命令:

$ rspec spec/address_book_spec.rb models/address_book.rbrspec spec/address_book_spec.rb models/address_book.rb 

要傳遞到rspec的第二個參數是models/address_book.rbspec,這名不存在的文件。 RSpec無法加載不存在且發生此錯誤的文件。它看起來像你對我可以簡化您的rspec命令:

$ rspec spec/address_book_spec.rb 

沒有必要通過一個第二spec/address_book_spec.rb ARG像你在幹什麼。您也不應該將實現文件(models/address_book.rb)的路徑傳遞給RSpec。相反,您應該在spec/address_book_spec.rb的頂部require 'models/address_book.rb