Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Commit

Permalink
Added integration tests for rescuing
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jan 28, 2010
1 parent 47908c9 commit 6fbbe2b
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 14 deletions.
7 changes: 3 additions & 4 deletions Rakefile
Expand Up @@ -107,19 +107,18 @@ end

task :cucumber => [:gemspec, :vendor_test_gems]

OLD_RAILS_VERSIONS = RAILS_VERSIONS[0...-1]

namespace :cucumber do
namespace :rails do
OLD_RAILS_VERSIONS.each do |version|
RAILS_VERSIONS.each do |version|
desc "Test integration of the gem with Rails #{version}"
task version do
puts "Testing Rails #{version}"
ENV['RAILS_VERSION'] = version
system("cucumber --format progress features/rails.feature")
end
end

desc "Test integration of the gem with all Rails versions"
task :all => [:cucumber, *OLD_RAILS_VERSIONS]
task :all => RAILS_VERSIONS
end
end
20 changes: 20 additions & 0 deletions features/rails.feature
Expand Up @@ -62,3 +62,23 @@ Feature: Install the Gem in a Rails application
And I configure my application to require the "hoptoad_notifier" gem
And I run "script/generate hoptoad"
Then I should see "You must first remove the hoptoad_notifier plugin. Please run: script/plugin remove hoptoad_notifier"

Scenario: Rescue an exception in a controller
When I generate a new Rails application
And I configure the Hoptoad shim
And I configure my application to require the "hoptoad_notifier" gem
And I run "script/generate hoptoad -k myapikey"
And I define a response for "TestController#index":
"""
session[:value] = "test"
raise RuntimeError, "some message"
"""
And I perform a request to "http://example.com:123/test/index?param=value"
Then I should receive the following Hoptoad notification:
| component | test |
| action | index |
| error message | RuntimeError: some message |
| error class | RuntimeError |
| session | value: test |
| parameters | param: value |
| url | http://example.com:123/test/index?param=value |
71 changes: 70 additions & 1 deletion features/step_definitions/rails_application_steps.rb
Expand Up @@ -117,4 +117,73 @@

When /^I install the "([^\"]*)" plugin$/ do |plugin_name|
FileUtils.mkdir_p("#{RAILS_ROOT}/vendor/plugins/#{plugin_name}")
end
end

When /^I define a response for "([^\"]*)":$/ do |controller_and_action, definition|
controller_class_name, action = controller_and_action.split('#')
controller_name = controller_class_name.underscore
controller_file_name = File.join(RAILS_ROOT, 'app', 'controllers', "#{controller_name}.rb")
File.open(controller_file_name, "w") do |file|
file.puts "class #{controller_class_name} < ApplicationController"
file.puts "def consider_all_requests_local; false; end"
file.puts "def local_request?; false; end"
file.puts "def #{action}"
file.puts definition
file.puts "end"
file.puts "end"
end
end

When /^I perform a request to "([^\"]*)"$/ do |uri|
uri = URI.parse(uri)
request_script = <<-SCRIPT
require 'cgi'
class CGIWrapper < CGI
def initialize(*args)
@env_table = {}
@stdinput = StringIO.new("")
super(*args)
end
attr_reader :env_table, :stdinput
end
cgi = CGIWrapper.new
cgi.env_table.update({
'HTTPS' => 'off',
'REQUEST_METHOD' => "GET",
'SERVER_ADDR' => #{uri.host.inspect},
'SERVER_PORT' => #{uri.port.inspect},
'REQUEST_URI' => #{uri.request_uri.inspect},
'QUERY_STRING' => #{uri.query.inspect}
})
require 'dispatcher' unless defined?(ActionController::Dispatcher)
Dispatcher.dispatch(cgi)
SCRIPT
File.open(File.join(RAILS_ROOT, 'request.rb'), 'w') { |file| file.write(request_script) }
@terminal.cd(RAILS_ROOT)
@terminal.run("./script/runner -e production request.rb")
end

Then /^I should receive the following Hoptoad notification:$/ do |table|
exceptions = @terminal.output.scan(%r{Recieved the following exception:\n([^\n]*)\n}m)
exceptions.should_not be_empty

xml = exceptions.last[0]
doc = Nokogiri::XML.parse(xml)

hash = table.transpose.hashes.first

session_key, session_value = hash['session'].split(': ')
param_key, param_value = hash['parameters'].split(': ')

doc.should have_content('//component', hash['component'])
doc.should have_content('//action', hash['action'])
doc.should have_content('//error/message', hash['error message'])
doc.should have_content('//error/class', hash['error class'])
doc.should have_content('//request/url', hash['url'])

doc.should have_content('//request/session/var/@key', session_key)
doc.should have_content('//request/session/var', session_value)
doc.should have_content('//request/params/var/@key', param_key)
doc.should have_content('//request/params/var', param_value)
end

3 changes: 3 additions & 0 deletions features/support/env.rb
@@ -1,3 +1,6 @@
require 'activesupport'
require 'nokogiri'

PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..')).freeze
TEMP_DIR = File.join(PROJECT_ROOT, 'tmp').freeze
RAILS_ROOT = File.join(TEMP_DIR, 'rails_root').freeze
Expand Down
2 changes: 2 additions & 0 deletions features/support/hoptoad_shim.rb.template
@@ -1,5 +1,7 @@
require 'sham_rack'

ShamRack.at("hoptoadapp.com") do |env|
xml = env['rack.input'].read
puts "Recieved the following exception:\n#{xml}"
["200 OK", { "Content-type" => "text/xml" }, "<notice/>"]
end
24 changes: 24 additions & 0 deletions features/support/matchers.rb
@@ -0,0 +1,24 @@
module Matchers
def have_content(xpath, content)
simple_matcher "have #{content.inspect} at #{xpath}" do |doc, matcher|
elements = doc.search(xpath)
if elements.empty?
matcher.failure_message = "In XML:\n#{doc}\nNo element at #{xpath}"
false
else
element_with_content = doc.at("#{xpath}[contains(.,'#{content}')]")
if element_with_content.nil?
found = elements.collect { |element| element.content }
matcher.failure_message =
"In XML:\n#{doc}\n" +
"Got content #{found.inspect} at #{xpath} instead of #{content.inspect}"
false
else
true
end
end
end
end
end

World(Matchers)
9 changes: 0 additions & 9 deletions test/notice_test.rb
Expand Up @@ -339,15 +339,6 @@ def stub_request(attrs = {})
end

should "extract data from a rack environment hash" do
# def hoptoad_request_data
# { :parameters => hoptoad_filter_if_filtering(params.to_hash),
# :session_data => hoptoad_session_data,
# :controller => params[:controller],
# :action => params[:action],
# :url => hoptoad_request_url,
# :cgi_data => hoptoad_filter_if_filtering(request.env),
# :environment_vars => hoptoad_filter_if_filtering(ENV) }
# end
# TODO: extract session data
# TODO: extract controller
# TODO: extract action
Expand Down

0 comments on commit 6fbbe2b

Please sign in to comment.