huesler-informatik My 2 cents

Rails - Testing rescue action in public behavior

Rails handles exceptions that occur in production mode by rendering the file public/500.html or one of the other two (404.html and 422.html) if appropriate. Generally this is a good default, but in some cases it is a requirement to redirect the user to, or render specific pages for different types of exceptions. For instance, when an application provides a JSON API, it would be bad to return a 500.html to the caller when something goes wrong.

There are generally two ways do this in Rails. Either overwrite rescue_action_in_public in your controller or use the newer rescue_from to map exceptions to a handler. Ryan Daigle wrote a short but comprehensive blog post on this topic.

However, testing this is a bit tricky. config.consider_all_requests_local needs to be set to false and local_request? should be set to false as well. Testing this manually in the browser is cumbersome and error prone, so I needed a way to verify the behavior with a controller test. A quick ack --ruby rescue_action_in_public actionpack-2.3.4 turned up /actionpack-2.3.4/test/controller/rescue_test.rb exactly what I needed.

As it turns out, this is actually quiet easy to test. Basically, there are four things to consider and you are good to go:

1. Simulate the production environment in a setup method

2. Create a dummy controller with actions that reproduce the errors you want to test

3. Generate a route for your dummy controller if you have deleted the default routes in routes.rb

4. Call those methods in your test cases

With these four steps we got most uses cases covered. I ran into a problem, where I wanted to test an error that occurred during template rendering and required some special exception handling code. This is what worked for me:

Quicktip - Search in your shell history

Another neat trick I keep forgetting. If you want to search your shell history, just type in control-r and then your search query. You can browse through the search results by repeatedly pressing control-r. A big time saver if you ask me.

Quicktip - Preview files directly from the terminal on OSX

This is quick tip week. Have you ever wanted to preview a file on OSX with quick view without leaving the console. Well here is how:

qlmanage -p file
So if you wanted to preview the file image.png you would type:
qlmanage -p image.png
If you want to see the thumbnail preview of a file instead, just use -t instead of -p. To close the window either click the cross on the upper right corner or hit control-c to cancel the process. Happy previewing!

Quicktip - Flush DNS cache on OSX systems

Just in case you want to flush the DNS cache on an OSX system for whatever reason, this command gets the job done:

    dscacheutil -flushcache
  

Quicktip - Access .plist files with RubyCocoa

As part of the automated deployment process for a cocoa application, I needed to access a .plist file to change some values before the application gets bundled and uploaded.

Since plist files are plain XML files, I could have used something like libxml to get the job done but since this script runs on a Mac, I have access to RubyCocoa. That spares me the XML parsing and writing and gives me a simple persistent dictionary instead. This is what I ended up with:

I think that is pretty neat. However, I ran into a small pitfall when I tried to compare an NSString with a regular expression. I got this warning:

'NSString#=~' doesn't work correctly. Because it returns byte indexes. 
    Please use 'String#=~' instead.

The solution is pretty simple, just call to_s on the dictionary values.