Why am I receiving a load error in my RSpec tests?

132 views Asked by At

I am receiving the following load error when running my RSpec tests:

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>'

I am not sure how and why a load error occurs? Here is some of my code:

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

I have included an entries.csv and an entries_2.csv within the same folder as the application.

1

There are 1 answers

0
Myron Marston On

The error is telling you that it can't load a particular file that you've told it to load. Notice the error:

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

And notice your rspec command:

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

The 2nd argument you are passing to rspec is models/address_book.rbspec, which names a file which does not exist. RSpec cannot load files that do not exist and this error occurs. It looks to me like you can simplify your rspec command to:

$ rspec spec/address_book_spec.rb

There's no need to pass a 2nd spec/address_book_spec.rb arg like you were doing. You also shouldn't be passing the path to the implementation file (models/address_book.rb) to RSpec. Instead, you should require 'models/address_book.rb at the top of spec/address_book_spec.rb.