user3097405
user3097405

Reputation: 823

How to use Capybara to check error messages from Simple Form?

I have the following test:

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

And _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 %>

In order to have the full error message, config/initializers/simple_form_bootstrap.rb has this:

b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }

And model post.rb:

class Post < ActiveRecord::Base
  validates :title, :content, presence: true
end

When I run rspec, I get:

expected to find text "Title can't be blank." in "Post has not been created. new post * TitleTitle can't be blank * ContentContent can't be blank"

The save_and_open_page shows:

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

Why is Capybara not considering any whitespace between the <label> and <span>?

Upvotes: 1

Views: 1996

Answers (1)

Taryn East
Taryn East

Reputation: 27747

Looks like it's just a small mistake. You have a full-stop in your capybara spec:

expect(page).to have_content `Title can\'t be blank.'

Whereas the error message is showing you that the full-stop is not there:

TitleTitle can't be blank

remove it from your spec and it should pass :)

Upvotes: 1

Related Questions