Ruby on rails 如何使用Capybara检查来自简单表单的错误消息?

Ruby on rails 如何使用Capybara检查来自简单表单的错误消息?,ruby-on-rails,ruby,rspec,capybara,simple-form,Ruby On Rails,Ruby,Rspec,Capybara,Simple Form,我有以下测试: scenario 'with invalid attributes' do click_button 'Create Post' save_and_open_page expect(page).to have_content 'Post has not been created.' expect(page).to have_content 'Title can\'t be blank.' expect(page).to have_content 'Conten

我有以下测试:

scenario 'with invalid attributes' do
  click_button 'Create Post'
  save_and_open_page
  expect(page).to have_content 'Post has not been created.'
  expect(page).to have_content 'Title can\'t be blank.'
  expect(page).to have_content 'Content can\'t be blank.'
end
\u form.html.erb

<%= simple_form_for(post) do |f| %>
  # post passed to simple_form_for is @post
  <%= f.input :title %>
  <%= f.input :content %>
  <%= f.button :submit, class: 'button' %>
<% end %>
和型号
post
.rb:

class Post < ActiveRecord::Base
  validates :title, :content, presence: true
end
保存和打开页面
显示:

<div class="form-group string required post_title has-error">
  <label class="string required control-label" for="post_title">
    <abbr title="required">*</abbr> Title
  </label>
  <input class="string required form-control" type="text" value="" name="post[title]" id="post_title" />
  <span class="help-block">Title can&#39;t be blank</span>
</div>

*头衔
头衔可以';不要空白

为什么水豚不考虑
之间的空格?

看起来这只是一个小错误。您的水豚规范中有一个句号:

expect(page).to have_content `Title can\'t be blank.'
然而,错误消息显示没有句号:

TitleTitle can't be blank

从您的规范中删除它,它应该会通过:)

您的水豚规范中有一个句号:
标题不能为空。
错误消息显示没有句号:
标题不能为空
从您的规范中删除它,它应该会通过。哦,天哪!是的,就是这样。我甚至没有看邮件结尾的句号。我只能看到标题不能…。。谢谢你想回答这个问题以便我接受你的回答吗?:)呵呵——我自己也做过同样的事情,这就是为什么我知道要去寻找它;)
expect(page).to have_content `Title can\'t be blank.'
TitleTitle can't be blank