Testing in Middleman

Does anyone here build test suites for their Middleman sites?

Over the last few weeks I’ve been building an interactive art catalogue in MM (I’ve written a bit about my process here). This project includes some fairly complex features on both the front-end (client-side search and filters, maps and deep-zoom images, etc.) as well as on the back-end (I just wrote a simple extension for generating a PDF version of the book on build).

As the site becomes more complex I am beginning to feel that the lack of a test suite (even an incomplete one) for all of these features is a problem.

I am fairly new at Ruby and programming generally, and most of what I know about testing is in the context of Rails. I would love to write a few unit tests for the logic in my extensions/helper methods, as well as a couple of integration tests to tie the whole thing together, but I don’t really know where to begin.

Are there any “best practices” for where to put tests, how to run them, or how to integrate them with the Middleman processes generally? Is there an easy way to drop in Minitest or Rspec and set up a bundle exec test command?

Would love to hear if anyone here has solved this problem in their own work.

2 Likes

For frontend code written in JavaScript, I have had success testing it using Karma and PhantomJS. The setup will just be how you would normally test JavaScript, with or without Middleman.

If you wish to run test suites as part of middleman build, you could, for example, do the following in config.rb:

after_build do |builder|
  exit 1 unless builder.run 'karma start karma.config.js --single-run'
end

In this setup, middleman build will exit with status code 1 if your test suites fail, so CI server can catch it. Hope that helps.

Thanks for this. I have not used Karma/Jasmine/etc. before but it sounds like now would be a good time to start.

Running the tests in an after_build hook in the config file sounds like a good approach, I think that was the hint I was looking for. Is there a comparable way to run a suite of tests while developing (maybe listening for a livereload event or something)?

In my projects, when JavaScript files live reload, the browser refreshes the page. Perhaps you can make a special page, ignored by middleman build, that runs test suites when the page loads?

I just discovered another approach that might be useful here. I’d still like to learn how to use Karma, etc. at some point, but for folks who are looking for ways to use Ruby testing tools like Rspec or Capybara in a seamless way, check this out:

Look in the /spec/ folder for the important stuff. This repo is a very simple example of an Rspec/Capybara suite integrated into a middleman site. I just copied some of this code into my own project and things are working.

The key is the spec_helper file that the author created does most of the work:

require "middleman"
require 'rspec'
require 'capybara/rspec'

Capybara.app = Middleman::Application.server.inst do
  set :root, File.expand_path(File.join(File.dirname(__FILE__), '..'))
  set :environment, :development
  set :show_exceptions, false
end

After that, you can add Integration tests the way the author has done here, and run them with bundle exec rspec. Pretty cool!