Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6389731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:26:26+00:00 2026-05-25T03:26:26+00:00

I want to test this route I made on Sinatra: get ‘/party’ do begin

  • 0

I want to test this route I made on Sinatra:

get '/party' do
  begin
    party_source.parties
  rescue Exceptions::SourceNotFoundError
    status 404
  rescue Exceptions::SourceInternalError
    status 503
  end
end

And I wrote this test (assume the party_source is accessible by the test, in the actual code it is):

require 'rack/test'
def test_correct_status_code_when_get_error_404
    source_404 = mock() 
    source_404.expects(:parties).with(nil).raises(Exceptions::SourceNotFoundError)
    MyApp.party_source = source_404 

    get '/party'
    assert_equal 404, last_response.status
end

When I run this test it fails because instead of getting 404 (my code) I get a status 500. No matter what exception I raise I always get and status 500, which I think is being generated by Sinatra or Rack.

How can I test this case?

Update

As I can understand it, the exceptions isn’t getting caught by my rescues blocks. Rack or Sinatra is getting it and handling the HTTP Status 500 response message.

I can’t understand how my rescue code block is being ignored.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:26:26+00:00Added an answer on May 25, 2026 at 3:26 am

    Here’s a short example, showing that you can test such an action:

    hello_sinatra.rb:

    require 'sinatra/base'
    
    class Hello < Sinatra::Base
      get '/party' do
        begin
          raise StandardError
        rescue StandardError
          status 404
        end
      end
    end
    
    
    Hello.run! if __FILE__ == $0
    

    sinatra_test.rb:

    $:.push('.')
    require 'hello_sinatra'
    require 'test/unit'
    require 'rack/test'
    
    ENV['RACK_ENV'] = 'test'
    
    class HelloTest < Test::Unit::TestCase
      include Rack::Test::Methods
    
      def app
        Hello
      end
    
      def test_correct_status_code_when_get_error_404
        get '/party'
        assert_equal 404, last_response.status
      end
    end
    

    However, something looks strange in your code. Can you try to replace MyApp.party_source = source_404 with app.party_source = source_404

    Update

    You’re only catching Exceptions::SourceNotFoundError and Exceptions::SourceInternalError, something else is likely going wrong in your mock, which gives the 500 error.

    Add a catchall at the end of your begin/rescue block using rescue Exception and you will quickly see where the problem is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in my controller and want to test this code line
Say I have a vector: (def data [Hello World Test This]) And I want
I have a string of test like this: <customtag>hey</customtag> I want to use a
i want the following functionality. input : this is test <b> bold text </b>
This is what i want to do: $line = 'blabla translate(test) blabla'; $line =
I want to do this in Actionscript: typeof(control1) != typeof(control2) to test if two
I have a dictionary: a = {'100':12,'6':5,'88':3,'test':34, '67':7,'1':64 } I want to sort this
I have the following XML <title> This is a <highlight>test</highlight> thanks. </title> and want
CGI-style perl scripts are hard to test in this style: def test_it_says_hello_to_a_person get '/home.pl',
We want test the JTAPI feature of our application. Are there any emulator for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.