Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / unit-testing

Rspec Pacman Formatter

5.00/5 (3 votes)
10 Mar 2018CPOL1 min read 5.6K  
Formatting your rails specs with rspec_pacman_formatter

Introduction

Rspec, one of the most popular unit testing frameworks for Ruby these days, gives you the possibility to tweak its default output via Formatters.

Formatters are Ruby classes that you can write to change the behaviour of rspec while it is printing test results in the terminal. Most formatters are packed in Ruby gems and are typically hosted at rubygems.org.

Background

I decided to start my own formatter because I discovered some interesting Unicode characters that I could use to simulate the characters of the classic game Pacman.

The characters I found are listed below:

Unicode character Pacman equivalent
https://www.compart.com/en/unicode/U+15E3 ᗣ Ghosts
https://www.compart.com/en/unicode/U+15E7 ᗧ Pacman
https://www.compart.com/en/unicode/U+2022 • Pacdot

Then if you add a bit of color to these characters, you can achieve something like:

Notice that when one of the specs fail, you will get a random ghost between Blinky, Pinky, Inky or Clyde.

Using the Code

The code is available here and all you need to do is:

  1. Adding the gem dependency in the Gemfile:
    group :test do
      gem 'rspec_pacman_formatter'
    end
  2. Run:
    bundle
  3. Run specs with the --format flag to use the formatter inside rspec_pacman_formatter gem.
    rspec --format RspecPacmanFormatter::Pacman

Points of Interest

This was my first gem that I published on rubygems.org and it has more than 2k downloads already.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)