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

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

错误

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.

请帮忙解决这个问题...

assert_select "tag", "String" 仅匹配 <tag>String</tag>。如果你想要部分匹配,你可以使用正则表达式 assert_select "tag", /String/,匹配 <tag>a String</tag>.

在你的情况下这应该有效:

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