Last Updated: February 25, 2016
·
2.661K
· michiels

Integration testing Rake tasks

For one of our client projects, we heavily depend on a few Rake tasks that run to fetch data from SAP (ugh!) and other internal APIs (yay!). For instance, SAP holds a lot of inventory and stock data that we need to get into our Rails app.

At first, we didn't test these things at all. Hell has opened up multiple times since then.

Then, we tried testing with Cucumber and some kind of Rake runner. This is ok, but having all kinds of Rspec matchers and Cucumber step definitions just to test an import task is overkill and costs way too much energy as opposed to a simple Rails integration test.

So, finally, I ended up with something new and simpler. I ditched the Rake task runner and just shell out with backticks to start the rake task. Which is fine, since this is also the way Cron runs the task on our server environments.

I put it all in a simple integration test. Here is a simple of one of those test cases:

 test "product should be added with category SAP new when present in SAP export and not in VacuStock" do
  `bundle exec rake import_stock_from_sap:import`

  product = Product.find_by_shop_code('0000000001')
  assert_not_nil product, "Expected product New Bag to Import to be created"
  assert_equal 'New Bag to Import WITH STOCK!', product.title
  assert_equal 400, product.stock_from_sap

  category = Category.find_by_title('SAP new')
  assert_equal [category], product.categories
end

This test case tests if a new product from the SAP export is created accordingly with some extra meta data.

Simple! No more hassle.