Search code examples
ruby-on-railsrspecruby-on-rails-5rspec-railsruby-on-rails-6

Failure/Error: let(:completed_task) { Task.create!(completed_at: 1.day.ago, size: 1) }


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>

Solution

  • 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