Class: RspecShnork::RspecShnork

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_shnork_matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(thresholds, path = nil) ⇒ RspecShnork

Returns a new instance of RspecShnork.



13
14
15
16
# File 'lib/rspec_shnork_matchers.rb', line 13

def initialize thresholds, path = nil
  @path = path || '/'
  @thresholds = thresholds
end

Instance Method Details

#failure_messageObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec_shnork_matchers.rb', line 23

def failure_message
  max_messages = 5
  msg = <<-EOM
  Shnork found #{messages_to_string(404)} and #{messages_to_string(500)}.
  One or both of these are over the accepted thresholds (404 -> #{@thresholds[404]}, 500 -> #{@thresholds[500]}).
  EOM
  [404, 500].each do |error|
    i = 0
    if messages_for(error) > 0 then
      show_errors = messages_for(error) < max_messages ? messages_for(error) : max_messages
      msg += "      First #{show_errors} discovered #{error} error#{ show_errors == 1 ? '' : 's'}:\n"
      while i < max_messages and i < messages_for(error)
        msg += "      * " + @results.codes[error][i] + "\n"
        i += 1
      end
    end
  end
  return msg
end

#matches?(target) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/rspec_shnork_matchers.rb', line 18

def matches?(target)
  @results = Shnork::Hunt(@path)
  messages_for(404) <= @thresholds[404] and messages_for(500) <= @thresholds[500]
end

#messages_for(error) ⇒ Object



43
44
45
46
# File 'lib/rspec_shnork_matchers.rb', line 43

def messages_for(error)
  return 0 unless @results.codes.has_key? error
  return @results.codes[error].size
end

#messages_to_string(error) ⇒ Object



48
49
50
51
# File 'lib/rspec_shnork_matchers.rb', line 48

def messages_to_string(error)
  err = (messages_for(error) == 1) ? "error" : "errors"
  "#{messages_for(error)} #{error} #{err}"
end

#negative_failure_messageObject



53
54
55
# File 'lib/rspec_shnork_matchers.rb', line 53

def negative_failure_message
  "expected shnork not to pass"
end