Every time i start writing code for a new project i have the opportunity to try new tools, solutions and technologies. Currently, i am working at a RESTful API for marketcloud.it and apio.cc and it’s the right time for trying a new node module to write unit tests: frisbyjs.

As the website reads:

Frisby is a REST API testing framework built on node.js and Jasmine that makes testing API endpoints easy, fast, and fun.

And what i can say is : mission complete! testing with frisbyjs is really easy and fast! (Honestly i am not a big fan of test writing… ;) Honestly i didn’t know about frisby before reading about it in a blog post (thanks Prismatic) but when i read “based on Jasmine” i realized that the foundations were rock solid.

For those who don’t know Jasmine, it is a Behaviour Driven Development (BDD) framework for testing Javascript, a pretty famous one, and if you don’t know what BDD is, let me simplify and tell you that it’s just TDD (Test Driven Development) done right.

Frisby makes testing api easy by providing an intuitive interface:

var frisby = require(‘frisby’);

.get(‘

.expectStatus(200)

.expectHeaderContains(‘content-type’, ‘application/json’)

.toss(); frisby.create(‘Marketcloud API — Product Api test: GET /api/products’).get(‘ http://127.0.0.1/api/products?store=MyTestStore' .expectStatus(200).expectHeaderContains(‘content-type’, ‘application/json’).toss();

Since our api will use HTTP status codes to communicate the results of the interrogation, it’s very easy to check for errors. This is a very basic example, but frisbyjs also makes response testing easy:



.get(‘

.expectStatus(200)

.expectHeaderContains(‘content-type’, ‘application/json’)

.expectJSON({

“name” : “test product”,

“store” : “TestStore”

})

.toss(); frisby.create(‘Marketcloud API — Product Api test: GET /api/products’).get(‘ http://127.0.0.1/api/products?store=MyTestStore' .expectStatus(200).expectHeaderContains(‘content-type’, ‘application/json’).expectJSON({“name” : “test product”,“store” : “TestStore”}).toss();

By calling expectJSON() we ensure that the response object has certain properties and that those properties take the expected value.

You can find the full api documentation for frisbyjs here http://frisbyjs.com/docs/api/

For more informations about Jasmine