Question: Rails Validtions and error messages w/ Capybara

Question

Rails Validtions and error messages w/ Capybara

Answers 1
Added at 2016-12-28 18:12
Tags
Question

I am using Capybara for TDD w/ RSpe. My code executes fine, however, I am confused about a concept regarding error messages and Capybara:

In my spec file under scenario "A user fails to create a new Article", I have 3 expect(page).to have_content methods:

The first of those messages is written explicitly in my articles_controller file, however, the other two messages are written nowhere in my views or my controller and yet somehow my test still passes!

Why is this? Is it because of the default error message given for my Article.rb model validations match exactly what my spec expects my page to have?

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

My controller:

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

My Views:

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

And finally, my model:

class Article < ApplicationRecord
    validates :title, presence: true
    validates :body, presence: true
end
Answers
nr: #1 dodano: 2016-12-28 20:12

In your view you have

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

which iterate through all @article errors returned from Rails. As you validate presence of both title and body in Article model if the attributes are not present the error messages are displayed in the view after clicking Create Article button.

Source Show
◀ Wstecz