无法通过seed.db的测试

时间:2015-12-16 21:23:50

标签: ruby-on-rails ruby rspec

我的seed.db文件如下:

today = Date.today
next_due = today + 1.year

User.destroy_all
TodoList.destroy_all
TodoItem.destroy_all

User.create! [
  { username: "Fiorina", password_digest: "xyx123" },
  { username: "Trump", password_digest: "xyx123" },
  { username: "Carson", password_digest: "xyx123" },
  { username: "Clinton", password_digest: "xyx123" },
]

Profile.create! [
  { first_name:"Carly", last_name: "Fiorina", gender: "female", birth_year: 1954, created_at: "", updated_at: "", user_id: 1 },
  { first_name:"Donald", last_name: "Trump", gender: "male", birth_year: 1946, created_at: "", updated_at: "", user_id: 2 },
  { first_name: "Ben", last_name: "Carson", gender: "male", birth_year: 1951, created_at: "", updated_at: "", user_id: 3 },
  { first_name: "Hillary", last_name: "Clinton", gender:"female", birth_year: 1947, created_at: "", updated_at: "", user_id: 4 }
]

TodoList.create! [
  { list_name: "Something1", list_due_date: next_due, created_at: "", updated_at: "", user_id: 1 },
  { list_name: "Something2", list_due_date: next_due, created_at: "", updated_at: "", user_id: 2 },
  { list_name: "Something3", list_due_date: next_due, created_at: "", updated_at: "", user_id: 3 },
  { list_name: "Something4", list_due_date: next_due, created_at: "", updated_at: "", user_id: 4 }
]

(1..5).each do |item|
  TodoItem.create! [
    { title: "Task 1", due_date: next_due, description: "very important task TEST#{item}", todo_list_id: 1, completed: false },
    { title: "Task 2", due_date: next_due, description: "do something else TEST2#{item}", todo_list_id: 2, completed: true },
    { title: "Task 3", due_date: next_due, description: "do something else TEST3#{item}", todo_list_id: 3, completed: true },
    { title: "Task 4", due_date: next_due, description: "do something else TEST4#{item}", todo_list_id: 4, completed: true }
  ]
end

测试文件是:

context "rq09" do
  context "check seed file" do
    user_list = [
      [ "Carly", "Fiorina", "female", 1954 ],
      [ "Donald", "Trump", "male", 1946 ],
      [ "Ben", "Carson", "male", 1951 ],
      [ "Hillary", "Clinton", "female", 1947 ]
    ]

    before do
      User.destroy_all
      TodoList.destroy_all
      TodoItem.destroy_all
      Profile.destroy_all
      load "#{Rails.root}/db/seeds.rb"
    end

    it "has a file for seeding the database" do
      expect(File).to exist("#{Rails.root}/db/seeds.rb")
    end
    it "must have Users with lastnames for usernames as directed in assignment" do
      expect(User.all.to_a.length).to be(4)
      expect(User.all.map {|x| x.username }).to include("Trump", "Fiorina", "Carson", "Clinton")
    end

    it "must have Profiles set up for each user with the given data" do
      expect(Profile.all.length).to be(4)
      user_list.each do |fname, lname, gender, byear|
        p = Profile.find_by(last_name: lname)
        expect(p.first_name).to eql(fname)
        expect(p.gender).to eql(gender)
        expect(p.birth_year).to eql(byear)
      end
    end

    it "must have TodoList set up as directed" do
      expect(TodoList.all.length).to be(4)
      user_list.each do |fname, lname, gender, byear|
        expect(TodoList.find_by(user: User.find_by(username: lname))).to_not be_nil
      end
    end

    it "must have TodoItems set up as directed" do
      expect(TodoItem.all.length).to be(20)
      user_list.each do |fname, lname, gender, byear|
        user = User.find_by(username: lname)
        expect(user.todo_items.count).to be(5)
      end
    end
  end
end

然而,当我在rspec时,我得到以下结果:

Failure/Error: expect(TodoList.find_by(user: User.find_by(username: lname))).to_not be_nil

expected: not nil
got: nil

./spec/assignment_spec.rb:229:in `block (5 levels) in <top (required)>'
./spec/assignment_spec.rb:228:in `each'
./spec/assignment_spec.rb:228:in `block (4 levels) in <top (required)>'
./spec/assignment_spec.rb:12:in `block (2 levels) in <top (required)>'

当我查看数据库并使用rails控制台时,我看到我的用户ID不同(不再是1,2,3,4)。由于rake db:seed多次删除并重新创建了几行用户ID。因此,我无法动态地将用户ID与TodoList相关联。所以我的问题是如何验证seed.db文件todoList数据?

1 个答案:

答案 0 :(得分:1)

由于您未在id创建时指定User,因此假设特定用户将具有特定id没有意义,因为Rails不会在这方面。

在创建user_id时,您应该通过提供字面整数来提供TodoList值,而应该引用相关用户的id,如下所示:

{ list_name: "Something1", list_due_date: next_due, created_at: "", updated_at: "", user_id: User.find_by_username('Fiorina').id }

或者,当然,您可以在创建变量时将用户保存在变量中,以便以后更容易引用它们。如果您正在使用关联,则还可以按其关联名称引用它们,而不是按照id引用它们,如下所示:

{list_name:“Something1”,list_due_date:next_due,created_at:“”,updated_at:“”,user:User.find_by_username('Fiorina')}