September 5th, 2006
Sometimes, I just want to skip just that one test when I run my tests with rake test because it takes much longer than other tests, and it gets in the way of rapid iterative cycles when coding. I looked around and haven’t found a way to do that with rake, so I set down to writing a custom rake task.
namespace :test do
namespace :units do
Rake::TestTask.new(:excluding => 'db:test:prepare') do |t|
if ENV['THESE']
def t.file_list
exclusion_patterns = ENV['THESE'].split(',')
unit_tests = FileList['test/unit/**/*_test.rb'].select do |path|
excluded = exclusion_patterns.select { |excl| /#{excl}/.match(path) }
p "Excluding #{path}" if not excluded.empty?
excluded.empty?
end
end
else
p 'Running all unit tests (specify THESE="pattern1, pattern2" to exclude files)'
t.pattern = 'test/unit/**/*_test.rb'
end
t.libs << 'test'
t.verbose = true
end
Rake::Task['test:units:excluding'].comment = 'Run unit tests with exclusions (specify exclusions with THESE="pattern1,pattern2")'
end
end
Of course, rake test:recent and rake test:uncommitted would suffice for most purposes, but there are times when I just want to not run that a particular test or tests because they just take too long (and by definition, get in the way) and I’m pretty sure they won’t be broken. (Always run all the tests before a commit though, don’t be naughty.)
You can run the task like this:
rake test:units:excluding THESE="item_test,atte\w*"
Update: As it turns out, the task descriptions weren’t being set correctly. Revision 5016 fixes this so rake -T shows the task descriptions properly.
September 3rd, 2006
I’ve been writing lots of functional tests lately and came across several unexpected gotchas. Seeing as how forgetful I am recently, I’ll jot them down here (and hopefully benefit anyone else who comes across the same problems).
Testing flash.now
Testing the contents of the flash.now, somewhat surprisingly, can’t be done with code like this:
assert_equal 'Unable to activate your account.',
flash.now[:error]
flash.now[:error] would be nil, in your functional test, and a quick lookup on Google brought me to HowToTestFlash.Now on the Rails wiki. Apparently, Rails renders the flash in the view and then proceeds to clear the contents of flash.now (so it isn’t accessible in your functional test). So, you’d have to test the contents of the rendered page, instead of testing the flash itself:
assert_tag :tag => 'div',
:attributes => { :class => 'flash error' },
:content => 'Unable to activate your account.'
Asserting cookies
This one’s particularly annoying - trying to assert that cookies are set by your controllers. Unlike what was written in the Rails manual on testing (granted, it’s rather well-known that the manuals are somewhat outdated), you cannot retrieve cookies using a Symbol as an index into the cookies hash in your functional tests.
cookies[:auth_token] = {
:value => session[:user].remember_token,
:expires => session[:user].remember_token_expires_at
}
assert cookies[:auth_token] # nil
assert cookies['auth_token'] # This works.
Thanks to Herry for pointing this out - it saved me an exercise in frustration.
Yet another cookie testing gotcha is testing for the deletion of cookies (example use case: deleting a users cookie after he logs out). So you may do this in your controller:
cookies.delete :auth_token
And expect this to pass in your test:
assert_nil cookies['auth_token']
But nope, instead of setting the cookie to nil, the cookie is actually emptied.
assert cookies['auth_token'].empty? # This works.
I just used wrote my own custom assertion (there is an assert_cookie plugin around but I didn’t think this justified adding another plugin to the application, not just yet):
def assert_no_cookie(cookie_name)
cookies[cookie_name].nil?
|| cookies[cookie_name].empty?
end
Update: as it turns out, assert_no_cookie and assert_cookie_equals exist as custom assertions in ActionPack (source), but have been deprecated in favor of the cookies collection. Still, looking up the cookies collection with a Symbol but having it return nil is unexpected behavior considering the inline documentation says it should work.
September 1st, 2006
So with the usual fickle-mindedness, I’ve changed my blog software from Typo to Mephisto. Well, it wasn’t so much based on a whim - there were some reasons I wanted to stop using Typo:
- For some reason, Typo is very unstable for me on my VPS with 160MB RAM. I deploy Typo on a mongrel_cluster of 2 mongrels, fronting it with Apache 2.2 + mod_proxy_balancer, and all 160MB RAM and 192MB of swap is used up whenever I post an article or sweep the cache. Comments also take noticeably longer to post on my blog than on other Typo blogs. I’m not sure what exactly I’ve done wrong.
- I often get an obscure NilClass error that screws up the front page whenever pages are regenerated (often caused by someone posting a comment or an article).
- Changing the blog’s design requires one to create or edit an existing Typo theme - I kinda missed WordPress’s Web-based template editing. This was a significant roadblock to me ever changing the look and feel of my Typo blog.
Coincidentally, my urge to swap to Mephisto coincided with the release of mephisto 0.6 (Immortus) just today, and after some minor hiccups, I managed to get Mephisto up and running with my articles imported from Typo. (Still couldn’t get Capistrano to play well though, which I’m guessing is due to Rails version conflicts - I guess I’ll just have to do with the release version.)
Documentation is rather sparse at the moment, especially on how to convert from Typo to Mephisto (I updated that wiki page while I was there). I dug around in Mephisto 0.6’s source and found the converters in vendor/plugins/mephisto_converter. Not sure if there is a web interface to this - either way, things worked almost perfectly just running this command:
script/runner "Mephisto.convert_from :typo" -e production
You need to create a typo entry in database.yml that points to your existing Typo database. Also, the conversion will fail if any of your Typo users and email addresses are the same as the default Mephisto user admin. To avoid this just change the Mephisto user(s) login and email to something else.
So there’s that, hopefully some time this year I’ll update the look and feel.