Click here to Skip to main content
12,359,102 members (33,134 online)
Click here to Skip to main content
Add your own
alternative version

Stats

4.3K views
26 downloads
Posted

Prolific: the New Way of Using Jasmine for Tests

, 4 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Let's see how to use prolific to make better, more effective jasmine tests

Introduction

I would like to let you have a look to Prolific, a javascript library that lives among jasmine.
You can find it here: https://github.com/Bitterbrown/prolific

It allows you to write more clean and readable tests and to save time avoiding repetitions. It's easy to use, just include the library after jasmine js file (and jquery) and you're ready to go.

Problem using "raw" Jasmine

When testing behaviours, it often happens to write several lines of code within your tests just to open a modal window or a side panel. When a new developer is going to have a look at your tests, it's going to be hard for him to understand what the behaviour flow is.

Let's take as example this jasmine test

describe "Logged in user", ->

  it "should have a logged in user", ->
    myapp.navigate "logout"
    myapp.doLogin userFixture
    myapp.restart()
    myapp.navigate "start-page"

    expect(myapp.currentUser.name).toBe userFixture.name

  it "app should show user icon", ->
    expect($("header .user-icon").size()).toBeGreaterThan 0

  it "should open the left user panel when user icon is clicked", ->
    $("header .user-icon").click()
    
    waits 500

    runs ->
      expect($("body .left-region .user-panel").is(":visible")).toBeThruthy()

Now imagine you have to test your app without a logged in user.

describe "Logged out user", ->

  it "should not have a logged in user", ->
    myapp.navigate "logout"
    delete myapp.currentUser
    myapp.restart()
    myapp.navigate "start-page"

    expect(myapp.currentUser).toBeUndefined()


  it "app should not show user icon", ->
    expect($("header .user-icon").size()).toBe 0

PROBLEM

Who is going to read this test to understand better the application will have to struggle to get the context out of the code, it's not fluid and comprehensive.

HOW TO SOLVE THE PROBLEM

It is needed to improve the readability of those spec suites, to make them as short as possible and to be more descriptive

Adding Prolific to your Jasmine suite

Prolific goal is exactly to solve the problem just shown. Let's try to rewrite those tests using assume method that Prolific expose

describe "Logged in user", ->

  it "app should show user icon", ->
    assume "app has user paolo,moretti logged in"
    
    assume "$ header .user-icon is an element and var myapp.currentUser.name is 'paolo'"

  it "should open the left user panel when user icon is clicked", ->
    assume "on click .user-icon then in .5 seconds $ .left-region .user-panel is :visible"

Let's test the logged out behaviour

describe "Logged out user", ->

  it "app should not show user icon", ->
    assume "app has no user logged in"
    
    assume "$ header .user-icon isnt an element and myapp.currentUser.name is undefined"

As you can see, the test is now shorter and more readable.

OVERVIEW

assume "app has user paolo,moretti logged in"
assume "app has no user logged in"

This, in Prolific, is called routine. Routines are used to avoid code repetition, they basically are a regular expression that is defined externally and that react to user input to set a contextual behaviour for you.

assume "$ header .user-icon is an element and var myapp.currentUser.name is 'paolo'"
assume "on click .user-icon then in .5 seconds $ .left-region .user-panel is :visible" 

This is the concept of sentences.

  • Every assume call is fed with one ore more sentences (concatenated by ' and ')
  • Every sentence is then parsed to find matchers which are used to understand the type of test you want to run.
  • Every matcher will parse the text to find getters, variables to be evaluated

Please have a look to the repo for comprehensive documentation:
https://github.com/Bitterbrown/prolific

License

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

Share

About the Author

Paolo Moretti
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 4 Jul 2014
Article Copyright 2014 by Paolo Moretti
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid