Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

DRYing up Jasmine Specs with Shared Behavior

Davis W. Frank
Sunday, May 29, 2011

When writing nicely encapsulated code and you have a group of objects that share an interface (pick your favorite patterns), you often want to share your test code as well. If the system models financial accounts with credits and debits, and you make a change to the reconciliation code, you want to run a core set of specs against all cash, loan, and credit card account objects.

People ask how to do this in JavaScript on the Jasmine mailing list every so often.

So here’s an example of “Shared Example Groups” in Jasmine.

This Game spec looks totally fine for a basic ball game. But what if I want to have multiple different types of ball games?

Here are specs for Basketball and (American) Football:

The specs related to the Game itself are inline. That doesn’t feel very DRY and really, we want to test the interface with the same code. We’re working in JavaScript. So we write a function.

Jasmine works by building up all the functions to execute at spec run time. When you call describe, the passed-in function is executed immediately. Inside the describe, the calls to beforeEach, afterEach and it queue up the passed-in functions for execution later, when you want to run your specs.

This means you can put all of your shared specs, including any beforeEach calls, in a function that can be executed as needed. Like this:

Each time sharedBehaviorForGameOf is called, the three before and two spec functions are queued up. Call it twice and you’ll wind up with four new specs in your Jasmine environment.

And since it’s a function, yes, you can pass parameters. The context parameter can be used to pass in a ball and a game for use in the shared specs. Since the “(shared)” describe function is called each time sharedBehaviorForGameOf is called, there will be a separate closure for the local ball and game.

So now we can refactor our specs to use our function.

This is a useful technique, but use it appropriately and with a little caution. Use it in situations for consistent testing, not just to DRY up your specs for DRY-ness’s sake.

Keep the varied behavior (like the field goal scoring above) outside your shared behavior function instead of using the parameters. The parameters should be for the object(s) under test, not their behavior.

And lastly, (as always) keep your mind on your JavaScript closures.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

6 Comments

  1. Morten Nielsen says:

    Thanks for posting this!

    Might help me reduce the spec I’m working on with several lines of code.

    July 18, 2011 at 2:36 am

  2. Sebastian says:

    Argh, Murphy’s Law, huh? Of course the interesting part is missing:

    “This means you can put all of your shared specs, including any beforeEach calls, in a function that can be executed as needed. Like this:”

    …?

    April 8, 2013 at 10:39 am

  3. Sebastian says:

    And I want to add that your commenting system doesn’t work or at least doesn’t provide any feedback if the posting was successful.

    April 8, 2013 at 10:41 am

    • Davis W. Frank
      Davis W. Frank says:

      Thanks. I’ll forward this along to the team.

      April 8, 2013 at 12:20 pm

  4. Scott Silvi says:

    The code block for sharedBehaviorForGameOf is missing. However I did find it on this SO post: http://stackoverflow.com/questions/5061512/whats-a-good-way-to-reuse-test-code-using-jasmine that references this article.

    May 22, 2013 at 2:20 pm

  5. Davis W. Frank
    Davis W. Frank says:

    Scott – thanks for the find. No clue why it wasn’t showing up. But it should be there now.

    May 22, 2013 at 5:50 pm

Add New Comment Cancel reply

Your email address will not be published.

Davis W. Frank

Davis W. Frank
San Francisco

Recent Posts

  • An Entirely Late Movember Wrap-Up
  • Mid-ish Movember
  • Getting Involved with Movember
Subscribe to Davis W.'s Feed

Author Topics

movember (2)
rails (16)
palm (6)
agile (14)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >