Skip Navigation LinksHome > View Post

Unit testing Mobile Services scripts (Day 7)

Many developers are big fans of unit testing. I’m a big fan of unit testing, especially when authoring algorithms or tight routines with few dependencies. The ROI for that kind of code is huge; in fact, in this situation a unit test is often the easiest and cheapest way to test your code. It’s not that often that I end up writing that kind of code when building Mobile Services (though it does happen) and when I do, I want unit tests.

The first time I did this I simply split my code into multiple functions and created a unit test to execute certain functions. Here’s an example using the Mocha testing framework for JS (id TDD mode)

var assert = require('assert');

// the function I'm testing
function add(a, b) {
    return a + b;
}

// my test suite for the calculator
suite('calculator', function() {
    // a test in my suite
    test('add', function() {
        // the test
        var result = add(1, 2);
        // my assertion
        assert.strictEqual(result, 3);
    })
});

If you’ve done any unit testing before, this should look familiar – the notion of suites containing tests that pass or fail. I can install mocha using npm (see this post to get you started with node and npm).

npm install –g mocha

And then you can run your tests

mocha test.js –ui tdd

And you’ll get your results:

image

Easy, and all green! My add function could easily be more complicated (insert your prime number or fibonacci code) and this is a great way to get any complicated functions working before I use them in my Mobile Service. One place where unit testing gets weird is async. Javascript async is typically handled using callbacks (especially in node) so let’s imagine if our add method was async:

function add(a, b, callback) {
    setTimeout(function() {
        callback(null, a + b);
    }, 500);
}

Note that I added a delay of 0.5s to make it _really_ async. Now to test this we need to tell Mocha when we’ve done waiting for the async and are happy to declare the test passed. Here’s our code:

suite('calculator', function() {
    test('add', function(done) {
        add(1, 2, function(e, result) {
            assert.strictEqual(result, 3);
            done();
        });
    })
});

Notice how we specify the ‘done’ parameter in the test function. This informs Mocha that this is an async test and it should wait for me to call ‘done()’. Smart eh?

image

Notice how this test took a while longer (just over 500ms). Now that we’re grounded in Mocha and testing functions – what if I wanted to test my actual scripts? You know, the insert and read methods, for example?

In this case, you will have to do a little mocking of the Mobile Service request object (and possibly the user and query objects, depending how complicated you want to go) but it’s pretty simple. Let’s recap the request object:

request.execute() – when called with no parameters means go ahead, I’m done with any changes to the request – get on with it!


request.execute({ success: fn }) – call my success function and don’t send the response to the client, the consumer will do that now


request.respond() – send a response to the client with the canonical HTTP status code and JSON body for this operation

As an example, imagine we have an insert script that encrypts the value of a particular property (text) and a read script that decrypts it – this means this value is encrypted at rest in your SQL database. How could we test that?

And there you have it, some tests that can execute against your Mobile Services scripts allowing you to develop locally and be really sure your stuff works. Combine this with this post and you are getting really deeply serious about your Mobile Service workflow.

This was Day 7 of the twelve days of ZUMO – we’ll be back again tomorrow for Day 8.

 
Josh Post By Josh Twist
23:22
27 Dec 2012

» Next Post: Generating your own ZUMO auth token (Day 8)
« Previous Post: A Mobile Services Log Watcher (Day 6)

Comments:

Posted by Mauro @ 14 Jan 2013 10:46
Hey Josh,

Thanks for the excellent series, I'm kind of new to the whole Node and Mobile Services scene, and I was wondering how would you mock other items, such as the tables object as in this example.

https://github.com/WindowsAzure/azure-mobile-services/blob/master/samples/doto/ServerScripts/devices.insert.js

in particular the following : var devices = tables.getTable('devices');

Thanks very much and thanks again :)

Mauro

Posted by Cherilynn @ 27 Apr 2013 20:13
<url>http://www.locateinsurdeals.com/|new york car insurance</url> 46399 <url>http://www.medspricechart.com/|buy viagra</url> :-( <url>http://www.carinsurcompanies.com/|car insurance</url> 4724 <url>http://www.pricesforedmeds.com/|order levitra</url> xfazd <url>http://www.carinsurquote.com/|auto insurance rates</url> 9002 <url>http://www.topinsurdealsonline.com/|new jersey car insurance</url> rkkp

Posted by Honeysuckle @ 27 Apr 2013 20:13
<url>http://www.autocoverageonline.net/|auto insurance</url> :((( <url>http://www.medstabs4you.com/|discount cialis</url> hcsdd <url>http://www.onlinecollegeseasily.com/|college online</url> >:] <url>http://www.listofcarquotes.com/|auto insurance online</url> 991 <url>http://www.carinsurquote.com/|cheapest auto insurance</url> rdn

Posted by Johnette @ 29 Apr 2013 16:57
<url>http://www.locateinsurdeals.com/|auto insurance quotes</url> kmy <url>http://www.carinsurcompanies.com/|car insurance</url> 25091 <url>http://www.getyourquotesonline.com/|car insurance quotes</url> >:-DDD <url>http://www.edmedsonline4you.com/|cialis</url>; bgarr <url>http://www.carinsurcompanies.net/|online car insurance</url> %-] <url>http://www.findbestinsurquotes.com/|auto insurance online</url> 8-((( <url>http://www.mdicamentsenligne.fr/|cialis</url>; 8-OO

Posted by chan luu bracelet @ 04 May 2013 04:21
Yes, you can make a super-dressy evening http://www.chanluustore.net/ work while the sun's still shining! Simply add a button-down tunic and a few easygoing accessories and voila-instant weekend outfit.Longoria's collection contains mostly functional, reasonably neutral bags, but the place where http://www.coachoutletvogue.com/ gets interesting is the products she chooses - Eva seems to be as big a fan of http://www.usitccoachpurses.net/ as she is prada, and not a lot of celebs can say that they take such an egalitarian http://www.marcjacobssale.co.uk/ approach to their wardrobes http://www.marcjacobsbagsstore.com/ .

Posted by Almena @ 06 May 2013 20:23
<url>http://www.medstabs4you.com/|cialis</url>; vecj <url>http://www.pricesforedmeds.com/|levitra</url>; wskq <url>http://www.insurcomparisonservice.com/|car insurance quotes</url> 102959 <url>http://www.comparebestquotes.net/|car insurance quotes</url> 44944 <url>http://www.mdicamentsenligne.fr/|cialis</url>; =))

Posted by Lyddy @ 10 May 2013 04:49
<url>http://www.autocoverageonline.net/|auto insurance</url> dyef <url>http://www.findcheapinsurproviders.com/|cheap california auto insurance</url> nia <url>http://www.locateinsurdeals.com/|auto insurance quotes</url> =-PP <url>http://www.carinsurcompanies.com/|car insurance</url> 64529 <url>http://www.shopforedtabs.com/|cheap viagra</url> hue <url>http://www.mdicamentsenligne.fr/|Ventes de cialis</url> wouus <url>http://www.carinsurquote.com/|auto insurance rates</url> oyziv

Posted by Matei @ 13 May 2013 11:00
<url>http://www.tratamientodelaed.es/|cialis</url>; 8P <url>http://www.comparecarquotesonline.com/|car insurance quotes</url> iyi <url>http://www.compareinsuroffersonline.com/|cheap auto insurance</url> 6403 <url>http://www.cheapinsurdealsfast.com/|insurance auto auctions</url> gknpnz <url>http://getinsurlist.com/|a auto insurance</url> ntkuj

Posted by Gracyn @ 23 May 2013 20:16
<url>http://autoinsurshopping.com/|auto owners insurance</url> >:DDD <url>http://www.searchinsurancedeals.com/|car insurance quote</url> 602 <url>http://www.edmedsonline4you.com/|price of cialis</url> 65024 <url>http://www.usaglobalquotes.com/auto-insurance-quotes-utah.html|auto insurance quotes UT</url> zmjz <url>http://www.topinsurdealsonline.com/|car insurance quotes</url> 02455 <url>http://www.yourcarinsurancequotes.net/|auto owners insurance</url> =O

Posted by Elmira @ 24 May 2013 14:56
<url>http://www.accesstomedssavings.com/|prednisone</url>; qme <url>http://autoinsurshopping.com/|cheap auto insurance</url> 369008 <url>http://www.usaglobalquotes.com/|auto insurance quotes</url> 519 <url>http://www.youredmedsinfo.com/|cialis online</url> 22508 <url>http://www.lookingforedmeds.com/|online store cialis</url> 055 <url>http://www.edmedsonline4you.com/|cialis</url>; arp <url>http://www.shopforedtabs.com/|buy pfizer viagra</url> 776

Post a comment:

Name  

E-mail (never shared)

URL

Comments  

Captcha ImageRefresh Image
What's this?
Enter code above

© 2005 - 2013 Josh Twist - All Rights Reserved.