CSV导入Ruby中的RSpec错误

时间:2015-10-03 17:21:35

标签: ruby csv methods rspec

我正在使用Ruby进行一项任务。我必须为一个方法编写RSPEC测试,该方法从我的address_book app中删除一个条目。还必须编写一个测试和一个从CSV导入5个条目的方法。在我的remove_entry方法中,当我运行Specs时,它说我有一个未定义的删除方法。我已经问了几个红宝石开发者,他们乍看之下无法弄明白。下一个错误是当我为我的CSV导入运行测试时..数据没有按正确的顺序导入。我已经花了几个小时一遍又一遍地浏览我的代码,并试图解决这个问题。我在我的智慧结束..任何帮助将不胜感激!

address_book.rb

require_relative 'entry'
require "csv"
class AddressBook
   attr_accessor :entries

   def initialize
     @entries = []
   end


   def add_entry(name,phone_number,email)
     index = 0
     @entries.each do |entry|
       if name < entry.name
         break
       end
       index += 1
     end
     @entries.insert(index, Entry.new(name, phone_number, email))
   end

   def import_from_csv(file_name)
     csv_text = File.read(file_name)
     csv = CSV.parse(csv_text, headers: true, skip_blanks: true)

     csv.each do |row|
       row_hash = row.to_hash
       add_entry(row_hash["name"], row_hash["phone_number"], row_hash["email"])
       #can you clarify what the above is doing
       #is the format of row_hash["name"] because it is iterating over a hash or because it is an array?
     end
   end

   def remove_entry(name,phone_number,email)
     @entries.each do |entry|
       if (name == entry.name) && (email ==  entry.email) && (phone_number = entry.phone_number)
         entry.delete #this line returns an error in my RSPEC test
       else
         p "Entry does not exist \n Please try again."
       end
     end
   end
 end

address_book_spec.rb

require_relative "../models/address_book"

RSpec.describe AddressBook do
  let(:book) {AddressBook.new} # => lets us use the book variable in every test

  describe "attributes" do
    it "should respond to entries" do
     # book = AddressBook.new # => Replaced by line 4
     expect(book).to respond_to(:entries)
   end

   it "should initialize entries as an array" do
     # book = AddressBook.new # => Replaced by line 4
     expect(book.entries).to be_a(Array)
   end

   it "should initialize entries as an empty array" do
     # book = AddressBook.new # => Replaced by line 4
     expect(book.entries.size).to eq(0)
   end
  end
  describe "#add_entry" do
    it "adds only a single entry to the Address Book" do
      # book = AddressBook.new # => Replaced by line 4
      book.add_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
      expect(book.entries.size).to eq(1)
    end
    it "adds the correct information to entries" do
      # book = AddressBook.new # => Replaced by line 4
      book.add_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
      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('augusta.king@lovelace.com')
    end
  end
  # added remove entry test
  describe "#remove_entry" do
    it "should remove a single entry" do
      # book = AddressBook.new # => Replaced by line 4
      book.add_entry('Austin Thesing', '800.445.8833','austin@thesing.xyz')
      expect(book.entries.size).to eq(1)

      book.remove_entry('Austin Thesing', '800.445.8833','austin@thesing.xyz')
      expect(book.entries.size).to eq(0)
    end
  end
  def check_entry(entry,expected_name,expected_phone_number, expected_email)
    expect(entry.name).to eql(expected_name)
    expect(entry.phone_number).to eql(expected_phone_number)
    expect(entry.email).to eql(expected_email)
  end
  describe "#import_from_csv" do
    it "import an entry from a CSV file" do
      book.import_from_csv("entries.csv")
      book_size = book.entries.size

      expect(book_size).to eq 5 #checks the size of the book
    end
    it "adds the first entry" do
      book.import_from_csv("entries.csv")
      entry_one = book.entries[0]
      check_entry(entry_one,"Mark Griffo","123456789","mark@bloc.com")
    end
    it "adds the second entry" do
      book.import_from_csv("entries.csv")
      entry_two = book.entries[1]
      check_entry(entry_two,"Natalie Griffo","123456789","natalie@bloc.com")
    end
    it "adds the third entry" do
      book.import_from_csv("entries.csv")
      entry_three = book.entries[2]
      check_entry(entry_three, "Steve Thesing", "8583878899", "steve@steve.com")
    end
    it "adds the fourth entry" do
      book.import_from_csv("entries.csv")
      entry_four = book.entries[3]
      check_entry(entry_four, "Haidee Thesing", "8584458833", "h@thesing.com")
    end
    it "adds the fifth entry" do
      book.import_from_csv("entries.csv")
      entry_five = book.entries[4]
      check_entry(entry_five, "Olivia Meers", "0987654321", "olivia@meers.com")
    end
  end
end

终端输出/规格失败

Austins-MacBook-Pro:address-bloc austinthesing$ rspec spec/address_book_spec.rb 
.....F.FFFFF

Failures:

  1) AddressBook#remove_entry should remove a single entry
     Failure/Error: book.remove_entry('Austin Thesing', '800.445.8833','austin@thesing.xyz')
     NoMethodError:
       undefined method `delete' for #<Entry:0x007f8e8c1dea08>
     # ./models/address_book.rb:37:in `block in remove_entry'
     # ./models/address_book.rb:35:in `each'
     # ./models/address_book.rb:35:in `remove_entry'
     # ./spec/address_book_spec.rb:45:in `block (3 levels) in <top (required)>'

  2) AddressBook#import_from_csv adds the first entry
     Failure/Error: expect(entry.name).to eql(expected_name)

       expected: "Mark Griffo"
            got: "Haidee Thesing"

       (compared using eql?)
     # ./spec/address_book_spec.rb:50:in `check_entry'
     # ./spec/address_book_spec.rb:64:in `block (3 levels) in <top (required)>'

  3) AddressBook#import_from_csv adds the second entry
     Failure/Error: expect(entry.name).to eql(expected_name)

       expected: "Natalie Griffo"
            got: "Mark Griffo"

       (compared using eql?)
     # ./spec/address_book_spec.rb:50:in `check_entry'
     # ./spec/address_book_spec.rb:69:in `block (3 levels) in <top (required)>'

  4) AddressBook#import_from_csv adds the third entry
     Failure/Error: expect(entry.name).to eql(expected_name)

       expected: "Steve Thesing"
            got: "Natalie Griffo"

       (compared using eql?)
     # ./spec/address_book_spec.rb:50:in `check_entry'
     # ./spec/address_book_spec.rb:74:in `block (3 levels) in <top (required)>'

  5) AddressBook#import_from_csv adds the fourth entry
     Failure/Error: expect(entry.name).to eql(expected_name)

       expected: "Haidee Thesing"
            got: "Olivia Meers"

       (compared using eql?)
     # ./spec/address_book_spec.rb:50:in `check_entry'
     # ./spec/address_book_spec.rb:79:in `block (3 levels) in <top (required)>'

  6) AddressBook#import_from_csv adds the fifth entry
     Failure/Error: expect(entry.name).to eql(expected_name)

       expected: "Olivia Meers"
            got: "Steve Thesing"

       (compared using eql?)
     # ./spec/address_book_spec.rb:50:in `check_entry'
     # ./spec/address_book_spec.rb:84:in `block (3 levels) in <top (required)>'

Finished in 0.0176 seconds (files took 0.08714 seconds to load)
12 examples, 6 failures

Failed examples:

rspec ./spec/address_book_spec.rb:40 # AddressBook#remove_entry should remove a single entry
rspec ./spec/address_book_spec.rb:61 # AddressBook#import_from_csv adds the first entry
rspec ./spec/address_book_spec.rb:66 # AddressBook#import_from_csv adds the second entry
rspec ./spec/address_book_spec.rb:71 # AddressBook#import_from_csv adds the third entry
rspec ./spec/address_book_spec.rb:76 # AddressBook#import_from_csv adds the fourth entry
rspec ./spec/address_book_spec.rb:81 # AddressBook#import_from_csv adds the fifth entry

1 个答案:

答案 0 :(得分:1)

首次测试失败,因为删除方法使用不正确:

entry.delete #this line returns an error in my RSPEC test

需要

@entries.delete(entry)

条目以乱码方式插入,因为您正在使用您想要的中断

next

(我想这个循环应该按字母顺序插入。)

break命令终止每个块的完整,因此如果有一个更高字母顺序的名称,则不再执行迭代,接​​下来只是跳到下一次迭代。

应该是它