Rails与Capybara的有效和错误消息

时间:2016-12-28 17:22:26

标签: ruby-on-rails rspec capybara ruby-on-rails-5

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

在我在场景&#34下的spec文件中;用户无法创建新文章",我有3个期望(页面).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

1 个答案:

答案 0 :(得分:1)

在你看来,你有

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

迭代从Rails返回的所有@article错误。当您在title模型中验证bodyArticle的存在时,如果属性不存在,则在单击Create Article按钮后,视图中会显示错误消息。