I recently wrote a rake task to update some values we’ve got stored in the database. The rake task itself isn’t important in this post, but testing it is.

We’ve got many untested rake tasks in the database, so when our senior dev suggested adding a test, I had to build ours from scratch.

I did a bit more whack-a-moling with error messages than I’d hoped, so here’s a template of that test, along with some details that might save you some time, next time you are writing tests for your rake tasks.

We’re in a not-new version of Rails, and using Minitest. I’ve anonymized it. Hope it’s useful!

# test/tasks/rake_task_file_test.rbrequire'test_helper'require'rake'classRakeTaskFileTaskTest<ActiveSupport::TestCasedescribe'namespace:task_name'dodefsetup@tt=Fabricate(:object_with_attributes_i_need_to_change)ApplicationName::Application.load_tasksRake::Task["namespace:task_name"].invokeendit"should change 'thing I don't want'"do@tt.reloadvalues=@tt.attribute_i_changedrefute_includesvalues,"thing I don't want"assert_includesvalues,"thing I do want"endendend

note: this page has 17Mb of gifs and images. I don’t really want to take the time to manually trim the gifs from >3Mb/each to <1Mb each, so I didn’t. If you’re on mobile, or trying to conserve data, you might want to come back to this one later.

I value my attention and focus. I resent strongly when someone tries to take it away from me.

Chapter 5: Concatenation without the || operator

I use MySQL at work, and MySQL doesn’t support the || operator for string concatenation.

So, in the book, an expression like:

ItemOne||ItemTwo

evaluates to ItemOneItemTwo

In MySQL, || is a logical operator, just like in Ruby, so to get the above evaluation, you’d need to use:

CONCAT(ItemOne,ItemTwo)

That would give you ItemOneItemTwo. Of course, string concatenation pairs well with spaces between the strings you’re trying to concatenate, so please know that the CONCAT function can take any number of arguments, for example: