CSV文件向后显示,RSpec失败

时间:2016-02-05 20:36:54

标签: ruby-on-rails ruby rspec

我是Ruby的新手,无法找到解决为什么entries_2.csv向后导入的解决方案。我的其他.csv导入似乎是相同的语法,并且工作正常。

以下是我的RSpec for .csv方法的一部分:

require_relative "../models/address_book"

RSpec.describe AddressBook do

let(:book) {AddressBook.new}

def check_entry(entry, expected_name, expected_phone_number, expected_email)
    expect(entry.name).to eq(expected_name)
    expect(entry.phone_number).to eq(expected_phone_number)
    expect(entry.email).to eq(expected_email)
end

describe "#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 eq 5

    end

    it "imports the 1rst entry" do

        book.import_from_csv("entries.csv")
        entry_one = book.entries[0]
        check_entry(entry_one, "Bill", "555-555-4854", "bill@blocmail.com")

    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-5415", "bob@blocmail.com")

    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-3660", "joe@blocmail.com")


    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-4646", "sally@blocmail.com")

    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-2036", "sussie@blocmail.com")

    end

    it "imports an additional 3 entries" do

        book.import_from_csv("entries_2.csv")
        book_size = book.entries.size

        expect(book_size).to eq 3

    end

    it "imports 1rst entry into additional csv" do

        book.import_from_csv("entries_2.csv")
        entry_one = book.entries[0]
        check_entry(entry_one, "Ralph", "222-222-2222", "ralph@comcast.net")

    end

    it "imports 2nd entry into additional csv" do

        book.import_from_csv("entries_2.csv")
        entry_two = book.entries[1]
        check_entry(entry_two, "Will", "333-333-3333","Will@comcast.net")

    end

    it "imports 3rd entry into additional csv" do

        book.import_from_csv("entries_2.csv")
        entry_three = book.entries[2]
        check_entry(entry_three, "Foobarr", "444-444-4444", "Foobarr@comcast.net")

    end
end

这是我的两个.csv文件:

entries.csv

name,phone_number,email
Bill,555-555-4854,bill@blocmail.com
Bob,555-555-5415,bob@blocmail.com
Joe,555-555-3660,joe@blocmail.com
Sally,555-555-4646,sally@blocmail.com
Sussie,555-555-2036,sussie@blocmail.com

entries_2.csv

name,phone_number,email
Ralph,222-222-2222,ralph@comcast.net
Will,333-333-3333,Will@comcast.net
Foobarr,444-444-4444,Foobarr@comcast.net

以下是我在查看RSpec时收到的错误:

故障:

1)来自csv的AddressBook#import将第一个条目导入到其他csv中      失败/错误:expect(entry.name).to eq(expected_name)

   expected: "Ralph"
        got: "Foobarr"

   (compared using ==)
 # ./spec/address_book_spec.rb:8:in `check_entry'
 # ./spec/address_book_spec.rb:142:in `block (3 levels) in <top (required)>'

2)来自csv的AddressBook#import将第二个条目导入到其他csv中      失败/错误:expect(entry.name).to eq(expected_name)

   expected: "Will"
        got: "Ralph"

   (compared using ==)
 # ./spec/address_book_spec.rb:8:in `check_entry'
 # ./spec/address_book_spec.rb:150:in `block (3 levels) in <top (required)>'

3)Addressbook#import从csv导入第3个进入额外的csv      失败/错误:expect(entry.name).to eq(expected_name)

   expected: "Foobarr"
        got: "Will"

   (compared using ==)
 # ./spec/address_book_spec.rb:8:in `check_entry'
 # ./spec/address_book_spec.rb:158:in `block (3 levels) in <top (required)>'

以0.05492秒完成(文件加载时间为0.2885秒) 17个例子,3个失败

为什么我的entries.csv RSpec正确,但我的entries_2.csv不正确?我错过了什么。

由于

2 个答案:

答案 0 :(得分:0)

假设AddressBook是一个定义了has_many :entries的ActiveRecord模型,那么您遇到的核心问题是的关系保证以任何特定顺序返回,除非你明确要求。所以这个:

book.entries[0]

实际上正在运行:

SELECT * FROM entries WHERE address_book_id = ?

然后获取该数组的第一个元素。由于上述查询未被排序,您获得的订单是任意的(正如您所看到的那样; 有时它反映了创建顺序,有时它不反映)。

您实际想要的是实际请求按创建顺序排列的记录的内容,例如:

book.entries.order('entries.id ASC')[0]

您还可以通过将order: 'id ASC'作为参数添加到has_many :entries定义中来实现。

答案 1 :(得分:0)

我终于找到了解决问题的方法。 CSV.parse按字母顺序排序.csv个文件,我不知道这个。此时我不确定如何操作该特定方法,但重新安排我的names文件中的.csv解决了我的问题。