Introduction

A quick overview of Specmonstah's purpose and a short sweet working example.

Purpose

Specmonstah (Boston for "Specmonster") lets you write test fixtures that are clear, concise, and easy to maintain. It's great for dramatically reducing test boilerplate.

Say you want to test a scenario where a forum post has gotten three likes by three different users. You'd first have to create a hierarchy of records for the post, topic, topic category, and users. You have to make sure that all the foreign keys are correct (e.g. the post's :topic-id is set to the topic's :id) and that everything is inserted in the right order.

With Specmonstah, all you have to do is write code like this:

(insert {:like [[3]]})

and these records get inserted in a database (in the order displayed):

[[:user {:id 1 :username "T2TD3pAB79X5"}]
 [:user {:id 2 :username "ziJ9GnvNMOHcaUz"}]
 [:topic-category {:id 3 :created-by-id 2 :updated-by-id 2}]
 [:topic {:id 6
          :topic-category-id 3
          :title "4juV71q9Ih9eE1"
          :created-by-id 2
          :updated-by-id 2}]
 [:post {:id 10 :topic-id 6 :created-by-id 2 :updated-by-id 2}]
 [:like {:id 14 :post-id 10 :created-by-id 1}]
 [:like {:id 17 :post-id 10 :created-by-id 2}]
 [:user {:id 20 :username "b73Ts5BoO"}]
 [:like {:id 21 :post-id 10 :created-by-id 20}]]

Without Specmonstah, you'd have to write something like this to achieve the same result:

Call me crazy, but I think (insert {:like [[3]]}) is better. The Specmonstah DSL communicates what's important about the scenario you're trying to test. It eliminates all the visual noise that results from having to type out the foreign key relationships. It's:

  • Clear - the intention of the code is not obscured by boilerplate

  • Concise - a compact DSL lets you elegantly express the data you need to work with in your test

  • Easy to maintain - Less code = less bugs. In the non-Specmonstah example you can imagine how easy it would be to introduce a typo or otherwise make a mistake that's hard to figure out

If you think writing code that's clear, concise, and easy to maintain is super cool 😎then read on! The next section will give you an example you can play with in the REPL. This guide also includes:

  • An infomercial that highlights more Specmonstah's cool features

  • A thorough tutorial that walks you through how to use Specmonstah

Short Sweet Example

We've got a big ol' tutorial to help you master Specmonstah, but if you're more the gimme fun now kind of person, then try out this little interactive example. First, clone Specmonstah:

Open examples/short-sweet/short_sweet.clj in your favorite editor and start a REPL. I've also included the code below in case for example you don't have access to a REPL because, say, you're in some kind of Taken situation and you only have access to a phone and you're using your precious battery life to go through this guide.

The first ~66 lines of code include all the setup necessary for the examples to run, followed by snippets to try out with example output. Definitely play with the snippets 😀 Can you generate multiple todos or todo lists?

Last updated

Was this helpful?