【问题标题】:CSV Import RSpec error in RubyRuby 中的 CSV 导入 RSpec 错误
【发布时间】:2015-12-31 17:42:19
【问题描述】:

我正在使用 Ruby 进行作业。我必须为从我的地址簿应用程序中删除条目的方法编写 RSPEC 测试。还必须编写一个测试和一个从 CSV 导入 5 个条目的方法。在我运行 Specs 的 remove_entry 方法中,它说我有一个未定义的删除方法。我已经问过几个 ruby​​ 开发人员,他们乍一看都搞不清楚。下一个错误是,当我为我的 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

【问题讨论】:

    标签: ruby csv methods rspec


    【解决方案1】:

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

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

    需要

    @entries.delete(entry)
    

    条目以乱码的方式插入,因为您在可能需要的地方使用了 break

    next
    

    (我认为循环应该按字母顺序插入。)

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

    应该是这样的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-06
      • 2015-11-21
      • 2018-05-11
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多