失败/错误:assert_select“tr> td”,:text => “MyString”.to_s,:count => 2

时间:2016-11-17 10:31:12

标签: ruby-on-rails ruby rspec rspec-rails

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

错误

  

失败/错误:assert_select“tr&gt; td”,:text =&gt; “MyString的” .to_s,   :count =&gt; 2        测试::单位:: AssertionFailedError异常:          &LT; “MyString的” &GT;预期但是          &LT; “未知\ n \吨\吨\吨\吨(MyString的)” &GT;假不是真的。

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

1 个答案:

答案 0 :(得分:1)

assert_select "tag", "String"仅与<tag>String</tag>匹配。如果您想要零件匹配,可以使用与assert_select "tag", /String/匹配的正则表达式<tag>a String</tag>

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

assert_select "tr>td", :text => /MyString/, :count => 2
相关问题