2016-12-28 16 views
1

我使用的水豚用於TDD W/RSpe。我的代碼執行得很好,但是,我很疑惑有關錯誤消息和水豚的概念:Rails的Validtions和錯誤消息w /水豚

在我的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 
+0

這是因爲您的文章模型包含驗證代碼。 – 31piy

回答

1

在你看來,你有

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

它通過Rails中返回的所有@article錯誤迭代。正如在Article模型同時驗證titlebody的存在,如果屬性不存在的錯誤消息被點擊Create Article按鈕之後顯示在視圖中。