0

I added some test to spec/views/projects/index.html.erb_spec.rb file. Maybe because of version change I'm getting the error. The code is written for Rails 5, and I'm using Rails 6.

Here's the error I'm facing:

Failures:

  1) projects/index renders the index page with correct dom elements
     Failure/Error: let(:completed_task) { Task.create!(completed_at: 1.day.ago, size: 1) }
     
     ActiveRecord::NotNullViolation:
       SQLite3::ConstraintException: NOT NULL constraint failed: tasks.project_id

2) adding a project allows a user to create a project with tasks
     Failure/Error: (Time.zone.today + projected_days_remaining) <= due_date
     
     ActionView::Template::Error:
       Infinity

Both of the errors are from the spec/views/projects/index.html.erb_spec.rb file.

Here's spec/views/projects/index.html.erb_spec.rb file:

require "rails_helper"

describe "projects/index" do
    let(:completed_task) { Task.create!(completed_at: 1.day.ago, size: 1) }
    let(:on_schedule) { Project.create!(
    due_date: 1.year.from_now, name: "On Schedule", tasks: [completed_task]) }
    let(:incomplete_task) { Task.create!(size: 1) }
    let(:behind_schedule) { Project.create!(
    due_date: 1.day.from_now, name: "Behind Schedule",
    tasks: [incomplete_task]) }
    it "renders the index page with correct dom elements" do
        @projects = [on_schedule, behind_schedule]
        render
        expect(rendered).to have_selector("#project_#{on_schedule.id} .on_schedule")
        expect(rendered).to have_selector("#project_#{behind_schedule.id} .behind_schedule")
    end
end

Here's app/views/projects/index.html.erb file:

<h1>All Projects</h1>
<table>
  <thead>
    <tr>
      <td>Project Name</td>
      <td>Total Project Size</td>
    </tr>
  </thead>
  <tbody>
    <% @projects.each do |project| %>
    <tr class="project-row" id="<%= dom_id(project) %>">
      <td class="name"><%= name_with_status(project) %></td>
      <td class="total-size"><%= project.size %></td>
    </tr>
    <% end %>
  </tbody>
</table>
munim
  • 33
  • 10
  • The reason you are getting is now if we have relationship, then it is automatically validated, mean Task has relationship with Project, so if there is no project provided then it will give error, that was not in previous version and we have to manualy put valdiation for it so change this line `let(:completed_task) { Task.create!(completed_at: 1.day.ago, size: 1, project: on_schedule) }` and make this line below on_schedule project line. – Kamal Panhwar Feb 23 '21 at 05:38
  • @KamalPanhwar still cannot resolve it. :( – munim Feb 23 '21 at 06:46
  • well it is being called on next line ` let(:incomplete_task) { Task.create!(size: 1) }` so change it to ` let(:incomplete_task) { Task.create!(size: 1, project: on_schedule) }` – Kamal Panhwar Feb 23 '21 at 09:19
  • @KamalPanhwar Still cannot resolve it. :( – munim Feb 23 '21 at 16:58
  • Well you have to tell us what error is coming, your response has no solution as no error mention after doing changes. – Kamal Panhwar Feb 23 '21 at 17:00
  • @KamalPanhwar After making the changes, I kept getting the same errors as mentioned in the description. – munim Feb 24 '21 at 08:50

1 Answers1

0

This is happening because have in new version requirement of project for each task, please change your file spec/views/projects/index.html.erb_spec.rb with following content, I am just doing few stuff, like I am making project but not adding task, and in task I am adding project, now normally it should be connected, but due to spec running, I am also connected them together in inside of it block.

# frozen_string_literal: true

require 'rails_helper'
describe 'projects/index' do
  let(:on_schedule) do
    Project.create!(
      due_date: 1.year.from_now,
      name: 'On Schedule'
    )
  end

  let(:behind_schedule) do
    Project.create!(
      due_date: 1.day.from_now,
      name: 'Behind Schedule'
    )
  end

  let(:completed_task) do
    Task.create!(
      completed_at: 1.day.ago,
      size: 1,
      project: on_schedule
    )
  end

  let(:incomplete_task) do
    Task.create!(
      size: 1,
      project: behind_schedule
    )
  end

  it 'renders the index page with correct dom elements' do
    on_schedule.tasks << completed_task
    behind_schedule.tasks << incomplete_task

    @projects = [on_schedule, behind_schedule]
    render
    within("#project_#{on_schedule.id}") do
      expect(rendered).to have_selector('.on_Schedule')
    end

    expect(rendered).to have_selector(
      "#project_#{on_schedule.id} .on_schedule"
    )

    expect(rendered).to have_selector(
      "#project_#{behind_schedule.id} .behind_schedule"
    )
  end
end

I converted my projects so I notice somehow I have to do one more change, but I think it won't required for your project, as I noticed project is not considering Infinity values so I modified method on_schedule? added following change in app/models/project.rb

  def on_schedule?
    return false if projected_days_remaining.nan?  || projected_days_remaining.infinite?
    (Time.zone.today + projected_days_remaining) <= due_date
  end
Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37