Module: LogErrorStub

Defined in:
lib/invoca/utils/log_error_stub.rb

Overview

Used by functional tests to track exceptions.

Defined Under Namespace

Classes: ExpectedExceptionNotLogged, UnexpectedExceptionLogged

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#exception_whitelistObject

Returns the value of attribute exception_whitelist.



22
23
24
# File 'lib/invoca/utils/log_error_stub.rb', line 22

def exception_whitelist
  @exception_whitelist
end

Instance Method Details

#clear_exception_whitelistObject



70
71
72
# File 'lib/invoca/utils/log_error_stub.rb', line 70

def clear_exception_whitelist
  @exception_whitelist = nil
end

#exception_filtered?(exception_data) ⇒ Boolean

Did the calling code call expects_exception on this exception?

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/invoca/utils/log_error_stub.rb', line 47

def exception_filtered?(exception_data)
  @exception_whitelist && @exception_whitelist.any? do |expectation|
    if expectation[0] === exception_data[:error]
      expectation[1][:found] += 1
      true
    end
  end
end

#expects_exception(pattern, options = {}) ⇒ Object

Call this from your test file to declare what exceptions you expect to raise.



59
60
61
62
63
64
65
66
67
68
# File 'lib/invoca/utils/log_error_stub.rb', line 59

def expects_exception(pattern, options = {})
  @exception_whitelist ||= []
  expected_count = options[:count] || 1
  options = {:expected => expected_count, :found => 0}
  if to_increment = @exception_whitelist.find {|ex| ex[0] == pattern}
    to_increment[1][:expected] += expected_count
  else
    @exception_whitelist << [pattern, options]
  end
end

#handle_stub_log_error(exception_data, always_raise = false) ⇒ Object

Gets called by ExceptionHandling::log_error in test mode. If you have called expects_exception then this function will simply note that an instance of that exception has occurred - otherwise it will raise (which will generally result in a 500 return code for your test request)



40
41
42
# File 'lib/invoca/utils/log_error_stub.rb', line 40

def handle_stub_log_error(exception_data, always_raise = false)
  raise_unexpected_exception(exception_data) if always_raise || !exception_filtered?(exception_data)
end

#setup_log_error_stubObject



9
10
11
12
# File 'lib/invoca/utils/log_error_stub.rb', line 9

def setup_log_error_stub
  clear_exception_whitelist
  stub_log_error unless respond_to?(:dont_stub_log_error) && dont_stub_log_error
end

#stub_log_errorObject

Call this function in your functional tests - usually first line after a “should” statement once called, you can then call expects_exception By stubbing log error, ExceptionHandling will keep a list of all expected exceptions and gracefully note their occurrence.



30
31
32
# File 'lib/invoca/utils/log_error_stub.rb', line 30

def stub_log_error
  ExceptionHandling.stub_handler = self
end

#teardown_log_error_stubObject



14
15
16
17
18
19
20
# File 'lib/invoca/utils/log_error_stub.rb', line 14

def teardown_log_error_stub
  ExceptionHandling.stub_handler = nil
  return unless @exception_whitelist
  @exception_whitelist.each do |item|
    add_failure("log_error expected #{item[1][:expected]} times with pattern: '#{item[0].is_a?(Regexp) ? item[0].source : item[0]}' #{item[1][:count]} found #{item[1][:found]}") unless item[1][:expected] == item[1][:found]
  end
end