Subrat Rout
Subrat Rout

Reputation: 95

Can't pass the test for seed.db

My seed.db file is following:

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

Test File is:

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

However, when I rspec I get following result:

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)>'

When I looked into database and use rails console I see my user id are different(Not 1,2,3,4 anymore). Due to several times rake db:seed there were few rows of user id deleted and recreated. So I can't associate with user id to TodoList dynamically. So my question is how to validate the seed.db file todoList data?

Upvotes: 0

Views: 140

Answers (1)

Peter Alfvin
Peter Alfvin

Reputation: 29389

Since you're not specifying an id on User creation, it doesn't make sense to assume a particular user will have a particular id, as Rails makes no assertions in this regard.

Instead of providing a user_id value by providing a literal integer when creating a TodoList, you should reference the id of the user in question, as in:

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

Alternatively, of course, you can save the users in variables when you create them, so that you can more readily reference them later. If you're using associations, you can also reference them by their association name, rather than by their id as in:

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

Upvotes: 1

Related Questions