Я использую Capybara для TDD w/RSpe. Мой код выполняется отлично, однако я запутался в концепции сообщений об ошибках и Capybara:Rails Сообщение об ошибках w/Capybara
В моем файле спецификации по сценарию «Пользователь не создает новую статью», у меня есть 3 ожидающих (страницы). To have_content Методы:
Первое из этих сообщений написано явно в моем файле article_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
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>
И, наконец, моя модель:
class Article < ApplicationRecord
validates :title, presence: true
validates :body, presence: true
end
Это потому, что ваша модель статьи содержит код для проверки. – 31piy