Zain Abbas Zain Abbas - 18 days ago 7
Ruby Question

Failure/Error: assert_select "tr>td", :text => "MyString".to_s, :count => 2

index.html.erb_spec.rb

require 'spec_helper'

describe "instructions/index" do
before(:each) do
assign(:instructions, Kaminari.paginate_array([
stub_model(Instruction,
:path => "MyString",
:content => "Content",
:creator_id => 1,
:modifier_id => 2,
:updated_at => "2011-03-15"
),
stub_model(Instruction,
:path => "MyString",
:content => "Content",
:creator_id => 1,
:modifier_id => 2,
:updated_at => "2011-03-15"
)
]).page(1))
end

it "renders a list of instructions" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => "MyString".to_s, :count => 2
assert_select "tr>td", :text => "Content".to_s, :count => 2
assert_select "tr>td", :text => 1.to_s, :count => 2
assert_select "tr>td", :text => 2.to_s, :count => 2
assert_select "tr>td", :text => "2011-03-15".to_s, :count => 2
end
end


index.html.erb

<% title "Help Contents" %>

<%= paginate @instructions %>

<table class="table table-striped table-bordered" id="tips">
<thead>
<tr>
<th>Path</th>
<th>Content</th>
<th>Creator</th>
<th>Modifier</th>
<th>Last Modified</th>
<th class="no_sort"><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
</thead>
<tbody>
<% @instructions.each do |instruction| %>
<tr>
<td>
<%= instruction.actual_path %><br />
<span class="smallesttext">(<%= instruction.path %>)</span>
</td>
<td><%= truncate_html(simple_format(instruction.content), :length => 30) %></td>
<td><%= instruction.creator.reverse_full_name if instruction.creator %></td>
<td><%= instruction.modifier.reverse_full_name if instruction.modifier %></td>
<td><%= l instruction.updated_at, :format => :pretty %></td>
<td>
<%= link_to t('.show', :default => t("helpers.links.show")),
help_path(instruction), :class => 'btn btn-mini btn-info' %>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_help_path(instruction), :class => 'btn btn-mini' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
help_path(instruction),
:method => :delete,
:confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
:class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>

<%= paginate @instructions %>

<%= link_to "New Help Item",
new_help_path,
:class => 'btn btn-primary' %>


instructions_controller.rb

class InstructionsController < ApplicationController
respond_to :html, :xml, :json

layout :single_column_layout

before_filter :admin_only, :except => [:show]

def index
@instructions = Instruction.includes(:creator,:modifier).page(params[:page])

respond_with(@instructions)
end
end


ERROR


Failure/Error: assert_select "tr>td", :text => "MyString".to_s,
:count => 2
Test::Unit::AssertionFailedError:
<"MyString"> expected but was
<"unknown\n\t\t\t\t(MyString)"> false is not true.


Please help to solve this issue ...

Answer

assert_select "tag", "String" matches only against <tag>String</tag>. If you want a part match you can use a Regex assert_select "tag", /String/, that matches against <tag>a String</tag>.

In your case this should work:

assert_select "tr>td", :text => /MyString/, :count => 2
Comments