Apipie example recording with Minitest

I’ve been using the excellent apipie gem on a new project I’m working on and I wanted to share my solution for example recording with Minitest. The gem documentation has an example of how to specify which functional tests should be shown as an Api example if you happen to be using Rspec but nothing for Minitest - what to do?

If you read the source of the example recorder it’s actually pretty smart - it loads the apipie_examples.json file of the previously recorded examples and only adds new ones or updates existing examples. With this knowledge I came up with an approach - I added a special string #apidoc to the test name of the tests I want included in my Api docs. Then I made this Rake task to run only my Api controller tests and filter which tests using the string:

task :api_examples do
  Dir["#{Rails.root}/test/controllers/api/*controller_test.rb"].each do |controller|
    pipe = IO.popen("APIPIE_RECORD=examples bundle exec ruby -Itest #{controller} -n /#apidoc/")
    while (line = pipe.gets); print line; end

Also make sure you have config.show_all_examples = 1 in your apipie initializer file.

That’s how I did and its been working nicely for me, let me know in the comments if you have a better solution!

Kevin Hughes's Picture

About Kevin Hughes

Developer @Shopify, Traveller and Ultimate Player

Ottawa, Canada http://kevinhughes.ca