2015-12-16 63 views
0

我seed.db文件如下:不能通过测试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我得到以下结果:

当我看着数据库和使用导轨控制台,我看到我的用户ID是不同的(不是1,2,3,4了)。由于多次rake db:seed,有几行用户标识被删除并重新创建。所以我不能将用户ID动态关联到TodoList。所以我的问题是如何验证seed.db文件todoList数据?

+1

我不认为你应该为你的测试数据使用'seed.rb'。改用Fixtures(http://guides.rubyonrails.org/testing.html)或Factory girl(http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md)等工具。这将帮助您创建一致的测试数据。 – Baldrick

回答

1

既然你不指定idUser的创作,就没有任何意义的假设一个特定的用户都会有一个特定的id,像Rails不作任何断言在这方面。

而是通过创建一个TodoList时提供面值整数提供user_id值,你应该参考相关用户的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')}

+0

谢谢彼得。有效。我传递了硬编码的身份证,所以得到了错误。 –

+0

@SubratRout太好了!假设你的问题已解决,请接受答案,以便其他人不花时间看。 –