Home > Net >  assert_select, failure to find desired text
assert_select, failure to find desired text

Time:09-27

I am unsure why assert_select ".title", text: "Title".to_s, count: 2, returns 0 results.

index.html.erb:

<p style="color: green"><%= notice %></p>

<h1>Events</h1>

<div id="events">
  <% @events.each do |event| %>
    <%= render event %>
    <p>
      <%= link_to "Show this event", event %>
    </p>
  <% end %>
</div>

<%= link_to "New event", new_event_path %>

_event.html.erb:

<div  id="<%= dom_id event %>">
  <p >
    Title:
    <%= event.title %>
  </p>

  <p >
    <strong>Description:</strong>
    <%= event.description %>
  </p>

  <p >
    <strong>Price:</strong>
    <%= event.price %>
  </p>

  <p >
    <strong>Date:</strong>
    <%= event.date %>
  </p>

</div>

views/events/index.html.erb_spec.rb:

require 'rails_helper'
require "date"

RSpec.describe "events/index", type: :view do
  before(:each) do
    assign(:events, [
      Event.create!(
        title: "Title",
        description: "Description",
        price: 2,
        date: Date.today
      ),
      Event.create!(
        title: "Title",
        description: "Description",
        price: 2,
        date: Date.today
      )
    ])
  end

  it "renders a list of events" do
    render
    assert_select ".title", text: "Title".to_s, count: 2
    assert_select ".description", text: "Description".to_s, count: 2
    assert_select ".price", text: 2.to_s, count: 2
    assert_select ".date", text: Date.today.to_s, count: 2
  end
end

Failure:

1) events/index renders a list of events
     Failure/Error: assert_select ".title", text: "Title".to_s, count: 2
     
     Minitest::Assertion:
       <Title> expected but was
       <Title:
           Title>..
       Expected: 2
         Actual: 0
     # ./spec/views/events/index.html.erb_spec.rb:24:in `block (2 levels) in <main>'

Console of debugger to check what assert_select ".title" returns:

(ruby) assert_select ".title"
[#<Nokogiri::XML::Element:0x8840 name="p" attributes=[#<Nokogiri::XML::Attr:0x8818 name="class" value="title">] children=[#<Nokogiri::XML::Text:0x882c "\n    Title:\n    Title\n  ">]>, #<Nokogiri::XML::Element:0x887c name="p" attributes=[#<Nokogiri::XML::Attr:0x8854 name="class" value="title">] children=[#<Nokogiri::XML::Text:0x8868 "\n    Title:\n    Title\n  ">]>]
(rdbg) 

Could someone suggest why assert_select and my search for "Title" is returning 0 results and not the expected 2? :)

CodePudding user response:

You might have to change the matching text from Title to Title: and similarly change Description to Description:. E.g.

it "renders a list of events" do
  render
  assert_select ".title", text: "Title:", count: 2
  assert_select ".description", text: "Description:", count: 2
  assert_select ".price", text: 2.to_s, count: 2
  assert_select ".date", text: Date.today.to_s, count: 2
end

CodePudding user response:

Many thanks - it looks like the following using a regex match actually works, though I am not sure if this is best practice:

  it "renders a list of events" do
    render
    assert_select ".title", /Title:/, count: 2
    assert_select ".description", /Description:/, count: 2
    assert_select ".price", /2/, count: 2
    assert_select ".date", /#{Date.today.to_s}/, count: 2
  end
  • Related