2015-12-16 60 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太好了!假設你的問題已解決,請接受答案,以便其他人不花時間看。 –