2016-12-28 14 views
1

TDD w/RSpeにはCapybaraを使用しています。しかし、私のコードはうまく実行されますが、私はエラーメッセージとCapybaraに関するコンセプトについて混乱しています。Rails Validation and Error messages with Capybara

スペックファイルのシナリオで「ユーザーが新しい記事を作成できませんでした」というメッセージが3つあります。メソッド:

最初のメッセージは、私のarticles_controllerファイルに明示的に書かれていますが、他の2つのメッセージは私のビューやコントローラには書き込まれません。

これはなぜですか?私の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 
+0

これは、商品モデルに検証用のコードが含まれているためです。 – 31piy

答えて

1

あなたは

を持っています
 <% if @article.errors.any? %> 
     <ul> 
      <% @article.errors.full_messages.each do |msg| %> 
       <li> 
        <%= msg %> 
       </li> 
      <%end%> 
     </ul> 
    <%end %> 

Railsから返されたすべてのエラー@articleを繰り返します。 titlebodyの両方が存在することを確認すると、Articleモデルに属性が存在しない場合は、Create Articleボタンをクリックするとエラーメッセージが表示されます。