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

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

我在 spec/views/projects/index.html.erb_spec.rb 文件中添加了一些测试。可能是因为版本更改,我收到了错误。代码是为 Rails 5 编写的,我使用的是 Rails 6.

这是我遇到的错误:

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

两个错误都来自 spec/views/projects/index.html.erb_spec.rb 文件。

这是 spec/views/projects/index.html.erb_spec.rb 文件:

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

这是 app/views/projects/index.html.erb 文件:

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

发生这种情况是因为每个任务都有新版本的项目要求,请用以下内容更改您的文件spec/views/projects/index.html.erb_spec.rb,我只是在做一些事情,比如我正在制作项目但没有添加任务,在任务中我正在添加项目,现在通常应该连接它,但由于规范 运行,我还在 it 块内将它们连接在一起。

# 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

我转换了我的项目,所以我注意到我必须再做一项更改,但我认为你的项目不需要它,因为我注意到项目没有考虑无限值,所以我修改了方法 on_schedule?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