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