Sha256: 4861418de93a9e07c94d9a3fbd9070405c430524594a94b8eef99ae3978c5fac

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require 'rubygems'

require 'test/unit'
require 'sinatra/base'
require 'rack/test'
require 'toadhopper/test/methods'

$:.unshift File.dirname(__FILE__) + "/../lib"
require 'sinatra/toadhopper'

# Stub the Toadhopper posting

def Toadhopper.post!(*args)
  instance_variable_set(:@last_post_arguments, args)
end

def Toadhopper.last_post_arguments
  instance_variable_get(:@last_post_arguments)
end

class TestReportErrorToHoptoad < Test::Unit::TestCase
  
  class AppThatGoesBoom < Sinatra::Base

    helpers Sinatra::Toadhopper

    set :raise_errors, false
    set :sessions, true

    set :toadhopper, :api_key => "apikey", :filters => /afilter/

    get "/:id" do
      session["id"] = "sessionid"
      raise "Kaboom!"
    end

    error do
      post_error_to_hoptoad!
      "Ouch, that hurt."
    end
  end
  
  include Rack::Test::Methods

  def app; AppThatGoesBoom end

  def setup
    get "/theid"
    @error, @options, @header_options = Toadhopper.last_post_arguments
  end

  def test_api_key_set
    assert_equal "apikey", Toadhopper.api_key
  end
  def test_filter_set
    assert_equal [/afilter/], Toadhopper.filters
  end

  def test_reported_error
    assert_equal RuntimeError, @error.class
    assert_equal "Kaboom!", @error.message
  end
  
  def test_options
    assert_equal ENV, @options[:environment]
    assert_equal "http://example.org/theid", @options[:request][:url]
    assert_equal({"id" => "theid"}, @options[:request][:params])
    assert_equal nil, @options[:request][:rails_root]
    assert_equal({:key => 42, :data => {"id" => "sessionid"}}, @options[:session])
  end
  
end

class TestFailsSilentWithoutApiKey < Test::Unit::TestCase
  
  class AppWithoutApiKey < Sinatra::Base
    helpers Sinatra::Toadhopper
    set :raise_errors, false
    get("/") { raise "Kaboom!" }
    error do
      post_error_to_hoptoad!
      "Error"
    end
  end
  
  include Rack::Test::Methods

  def app; AppWithoutApiKey end
  
  def test_doesnt_raise_an_error
    assert_nothing_raised { get "/" }
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
toadhopper-sinatra-0.4 test/test_report_error_to_hoptoad.rb