Sha256: a1e2bca8c7dfc5841f228d00d572195d5a0b2d40303fe14988809cfcfc86e4d4

Contents?: true

Size: 1.39 KB

Versions: 12

Compression:

Stored size: 1.39 KB

Contents

require 'minitest_helper'

class TestController < Rasti::Web::Controller
  CustomError = Class.new StandardError
  
  def test
    render.html 'Test HTML'
  end

  def exception
    raise 'Unexpected error'
  end

  def fail
    raise CustomError, 'Expected error'
  end

  rescue_from CustomError do |ex|
    render.status 500, ex.message
  end
end

describe Rasti::Web::Controller do
  
  it 'Action endpoint' do
    action = TestController.action :test
    env = Rack::MockRequest.env_for '/test'
    status, headers, response = action.call env

    action.must_be_instance_of Rasti::Web::Endpoint
    status.must_equal 200
    headers['Content-Type'].must_equal 'text/html; charset=utf-8'
    response.body.must_equal ['Test HTML']
  end

  it 'Invalid action' do
    error = proc { TestController.action :invalid }.must_raise RuntimeError
    error.message.must_equal "Undefined action 'invalid' in TestController"
  end

  it 'Rescue exception' do
    action = TestController.action :fail
    env = Rack::MockRequest.env_for '/fail'
    status, headers, response = action.call env

    status.must_equal 500
    response.body.must_equal ['Expected error']
  end

  it 'Unexpected exception' do
    action = TestController.action :exception
    env = Rack::MockRequest.env_for '/exception'
    
    error = proc { action.call env }.must_raise RuntimeError
    error.message.must_equal 'Unexpected error'
  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rasti-web-1.0.0 spec/controller_spec.rb
rasti-web-0.2.3 spec/controller_spec.rb
rasti-web-0.2.2 spec/controller_spec.rb
rasti-web-0.2.1 spec/controller_spec.rb
rasti-web-0.2.0 spec/controller_spec.rb
rasti-web-0.1.1 spec/controller_spec.rb
rasti-web-0.1.0 spec/controller_spec.rb
rasti-web-0.0.7 spec/controller_spec.rb
rasti-web-0.0.6 spec/controller_spec.rb
rasti-web-0.0.5 spec/controller_spec.rb
rasti-web-0.0.4 spec/controller_spec.rb
rasti-web-0.0.3 spec/controller_spec.rb