Sha256: 70ef8a65a06f7e95486f46a2b2930b9b8e9b841197afc19cb9164f1fad8ddad4

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

Spec::Matchers.define :contain do |item|
  match do |arry|
    arry.include?(item)
  end
end

Spec::Matchers.define :resemble do |item|
  match do |doc|
    case doc
      when ActionController::Response, String
        doc.tml_should_resemble(item)
      else
        doc.to_tml.tml_should_resemble(item)
    end
  end
end

class ResponseContainsErrors
  def initialize(exception_class = nil)
    @exception_class = exception_class
  end

  def matches?(response)
    @response = response
    @exception = @response.template.controller.instance_variable_get("@exception")
    if !@exception_class
      !@exception.nil?
    else
      @exception.kind_of?(@exception_class)
    end
  end

  def failure_message_for_should
    expected_to("contain", @exception_class) + ", found " + found_instead
  end

  def failure_message_for_should_not
    expected_to("not contain", @exception_class) + ", found " + found_instead
  end

  private
  def expected_to(what, which)
    "Expected response to #{what}" + (which ? " (#{which})" : " an error")
  end

  def found_instead
    @exception ? "#{@exception.class.to_s} <#{@exception.message}>" : "none"
  end
end

def contain_errors(expected = nil)
  ResponseContainsErrors.new(expected)
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rtml-2.0.3 spec/support/rspec/matchers.rb
rtml-2.0.2 spec/support/rspec/matchers.rb
rtml-2.0.1 spec/support/rspec/matchers.rb
rtml-2.0.0.alpha.1 spec/support/rspec/matchers.rb