jeudi 4 juin 2015

Mocha - Accessing named functions with ti-mocha

I'm using a hybrid version of mocha, ti-mocha, to build unit test for a Titanium SDK-based app. I'm completely new to BDD and mocha, so the API learning curve is quite steep. Here's my issue with a stripped-down test harness. I can access the index.js module, its methods and properties. However, I don't know how to access the named functions, in this case doClick(), within that module.

I'm using mocha + should.js. In the test harness below, context "index" passes, but context "doClick" fails. I'm so new to this API, that I'm not sure if I even framed the question properly. How do I access the functions within the module?

index-mocha.js

// creates the "mocha" global necessary to run a test suite anywhere in your app
var should = require('should');

module.exports = function(index) {
    // create the test suite
    describe('mochatest', function() {

        context('index', function() {

            it('index exists', function() {
                should.exist(index);
            });

            it('index.open function', function() {
                should(index.open).be.a.Function;
            }); 

            it('id = index', function() {
                index.id.should.equal('index');
            });
        });

        context('doClick', function() {

            it('doClick exists', function() {
                should.exist(index.doClick);
                // return;
            });

            it('doClick is a function', function() {
                should(index.doClick).be.a.Function;
            });
        });
    });

    var outputFile = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, 'results.json');
    outputFile.createFile();

    mocha.setup({
        reporter: 'ti-spec', // the reporter to use with your tests
        outputFile: outputFile, // write results to the given Ti.Filesystem.File file
        quiet: false // if true, suppress all console logging
    });

    // run the tests
    mocha.run();
    };

index.js

function doClick(e) {
    alert($.label.text);
}

if(runTests){
    require('ti-mocha');

    $.index.addEventListener('open', function(){
        require('index-mocha')($.index);
    });
}

$.index.open();

Aucun commentaire:

Enregistrer un commentaire