Rails 使用 Capybara 的验证和错误消息

Rails Validtions and error messages w/ Capybara

我正在使用 Capybara 进行带有 RSpe 的 TDD。我的代码执行得很好,但是,我对有关错误消息和 Capybara 的概念感到困惑:

在场景 "A user fails to create a new Article" 下的规范文件中,我有 3 个 expect(page).to have_content 方法:

这些消息中的第一条明确写入了我的 articles_controller 文件,但是,其他两条消息没有写入我的视图或我的控制器中的任何地方,但不知何故我的测试仍然通过了!

这是为什么?是因为为我的 Article.rb 模型验证提供的默认错误消息与我的规范期望我的页面具有的内容完全匹配吗?

require "rails_helper"

RSpec.feature "Creating Articles" do
    scenario "A user creates a new article" do
        visit "/"

        click_link "New Article"

        fill_in "Title", with: "Creating a blog" 
        fill_in "Body", with: "Lorem Ipsum" #Does Capybara test for the specific words "Lorem Ipsum," or does it test for the String Datatype?"

        click_button "Create Article"

        expect(page).to have_content("Article has been created")
        expect(page.current_path).to eq(articles_path)
    end

    scenario "A user fails to create a new article" do
        visit "/"

        click_link "New Article"

        fill_in "Title", with: "" 
        fill_in "Body", with: "" 

        click_button "Create Article"

        expect(page).to have_content("Article has not been created")
        expect(page).to have_content("Title can't be blank")
        expect(page).to have_content("Body can't be blank")
    end
end

我的控制器:

class ArticlesController < ApplicationController
  def index
  end

  def new
     @article = Article.new
  end

  def create
    @article = Article.new(article_params)
    @article.save

    if @article.save
        flash[:success] = "Article has been created"
         redirect_to articles_path
    else 
      flash[:danger] = "Article has not been created."
        render new_article_path #or it can be render :new
      end 
  end



  private

    def article_params
        params.require(:article).permit(:title, :body)
    end
end

我的观点:

<h3 class="text-center">Adding New Article</h3>

<div class="row">
  <div class="col-md-12">
    <%=form_for(@article, :html => {class: "form-control", role: "form"}) do |f| %>
        <% if @article.errors.any? %>
            <ul>
                <% @article.errors.full_messages.each do |msg| %>
                    <li>
                        <%= msg %>
                    </li>
                <%end%>
            </ul>
        <%end %>

        <div class="form-group">
            <div class="control-label col-md-1">
                <%= f.label :title %>
            </div>
            <div class="col-md-11">
                <%= f.text_field :title, class: 'form-control', placeholder: "Title of article", autofocus: true %>
            </div>
        </div>
        <div class="form-group">
            <div class="control-label col-md-1">
                <%= f.label :body %>
            </div>
            <div class="col-md-11">
                <%= f.text_area :body, rows: 10, class: 'form-control', placeholder: 'Body of article' %>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-1 col-md-11">
                <%= f.submit class: 'btn btn-primary btn-lg pull-right' %>
            </div>
        </div>

最后,我的模型:

class Article < ApplicationRecord
    validates :title, presence: true
    validates :body, presence: true
end

在你看来你有

       <% if @article.errors.any? %>
        <ul>
            <% @article.errors.full_messages.each do |msg| %>
                <li>
                    <%= msg %>
                </li>
            <%end%>
        </ul>
    <%end %>

循环遍历从 Rails 返回的所有 @article 错误。当您验证 titlebodyArticle 模型中是否存在时,如果属性不存在,则单击 Create Article 按钮后会在视图中显示错误消息。