The issue is that get /unknown_path'
is not actually raising an error – it's only receiving a 404 request.
From Sinatra: README
The error handler is invoked any time an exception is raised from a route block or a filter. The exception object can be obtained from the sinatra.error
Rack variable...
Your test is testing for an actual error (this is what raise_error
does), while Sinatra is squashing the error – otherwise, every time someone 404'd the server would crash!
Check out the Sinatra testing guide for better direction on forming your tests. The basic idea is that using get ...
in a test sets the last_response
local variable, which can then be tested for equality, etc.
last_response
has a status
attribute, so (like was mentioned in another answer) you can just test to make sure that last_response.status
equals 404.
Edit:
I wasn't really clear about this. In testing mode, the application does actually raise errors.
From Sinatra Settings
raise_errors
raise exceptions (will stop application). Enabled by default when environment is set to "test", disabled otherwise.
So you don't actually want to raise just any error, but raise Sinatra::NotFound.new("Not found!")
to raise the specific type of error. The Sinatra::NotFound
error will trigger the 404 handler. Sinatra Error Handling