簡體   English   中英

紅寶石。 運行驗證測試| 添加待辦事項會顯示錯誤

[英]Ruby. Running a validation test | Adding todo items displays an error

從create_spec.rb運行驗證測試時,出現以下錯誤。

bin/rspec --format=documentation spec/features/todo_items/create_spec.rb

Failures:

  1) Adding todo items displays an error with content less than 2 characters long
     Failure/Error: expect(page).to have_content("There was a problem adding that todo list item.")
       expected to find text "There was a problem adding that todo list item." in "There was a problem adding that todo list item"
     # ./spec/features/todo_items/create_spec.rb:41:in `block (3 levels) in <top (required)>'
     # ./spec/features/todo_items/create_spec.rb:40:in `block (2 levels) in <top (required)>'

  2) Adding todo items displays an error with no content
     Failure/Error: expect(page).to have_content("There was a problem adding that todo list item.")
       expected to find text "There was a problem adding that todo list item." in "There was a problem adding that todo list item"
     # ./spec/features/todo_items/create_spec.rb:30:in `block (3 levels) in <top (required)>'
     # ./spec/features/todo_items/create_spec.rb:29:in `block (2 levels) in <top (required)>'

這是我創建的spec.rb

require 'spec_helper'

describe "Adding todo items" do
    let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries" ) }

    def visit_todo_list(list)
       visit "/todo_lists"
       within "#todo_list_#{list.id}" do
       click_link "List Items"
   end
 end

it "is successful with valid content" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: "Milk"
    click_button "Save"
    expect(page).to have_content("Added todo list item.")
    within("ul.todo_items") do
     expect(page).to have_content("Milk")
    end
 end    

it "displays an error with no content" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: ""
    click_button "Save"
    within("div.flash") do
    expect(page).to have_content("There was a problem adding that todo list item.")
 end
    expect(page).to have_content("Content can't be blank")
 end

it "displays an error with content less than 2 characters long" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: "1"
    click_button "Save"
    within("div.flash") do
    expect(page).to have_content("There was a problem adding that todo list item.")
 end
    expect(page).to have_content("Content is too short")
 end
end

我在todo_item.rb中向模型添加了驗證:

class TodoItem < ActiveRecord::Base
  belongs_to :todo_list

    validates :content, presence: true,
                        length: { minimum: 2}
end

我將代碼添加到html的布局中:

<%= form_for [@todo_list, @todo_item] do |form| %>
<% if @todo_item.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@todo_item.errors.count, "error") %> prohibited this todo item from being saved:</h2>

      <ul>
      <% @todo_item.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


    <%= form.label :content %>
    <%= form.text_field :content %>

    <%= form.submit "Save" %>
    <% end %>

錯誤消息顯示:

expected to find text "There was a problem adding that todo list item." in "There was a problem adding that todo list item"

您期望句子以句號結尾,但顯然不是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM