Recently I’ve been refactoring legacy Test::Unit code to use shoulda. Sometimes you can’t factor out repetitive stuff into a context, but you still want to be DRY. Below is an example case of refactoring into a custom should. Note that you need to use class variables, and that the self.should_* method needs to be defined before it’s actually used, since shoulda is built upon metaprogramming which gets evaluated at the class level.
...
def self.should_request sym
sym = sym.to_s
context "when requesting #{sym}" do
setup do
get :download, {:id => @@podcast.id, :type => sym}, @@session
end
should "ask for a device" do
assert_match /Please select a device to download content/, @response.body
end
should "increment the message count when sent #add_to_device" do
assert_difference 'Message.count( :conditions => "device_id = 1")', @@podcast.episodes.size do
xhr :post, :add_to_device, {:id => 1, :type => 'all', :podcast_id => @@podcast.id}, @@session
end
end
end
end
context "A valid account" do
setup do
@account = accounts(:accounts_002)
@@session = {:account_id => @account.id}
@account.devices << Device.find(1)
@@podcast = podcasts(:joe_cartoon)
@@session.merge!({:podcast_episodes => @@podcast.episodes})
end
should_request :all
should_request :latest
should_request :episode
end
...


Leave a Reply