a

Thursday, 13 August 2015

Unit Testing

https://mochajs.org

Assertions

Reference


  1. Include Mocha Javascript Test Framework
  2. Include assert library
  3. import mocha in the controller you want to test. for example in "index.js" you can import using require('ti-mocha');
  4. Define Tests ( you can name it as <View Name>_Test. js example "index_Test.js

var should = require('should');

module.exports = function(win) {

   describe('app.js', function() {

      describe('#myWindow', function() {

            it('Window exists', function() {
                should.exist(win);
         
            });     
        
   });
});
    // run the tests
    mocha.run();
};

4. Include and Call the Test Module from the controller you want to test . 

require('index_Test')(<PassObject>);


To Capture Results at a seperate file rather than console


var outputFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'results.json');
Ti.API.info(Ti.Filesystem.applicationDataDirectory); // To get the App Data directory
outputFile.createFile();

mocha.setup({ 
    reporter: 'ti-json',    //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

});


To View the Results in console

Prasads-MacBook-Pro:~ prasadkatankot$ cd /Users/prasadkatankot/Library/Developer/CoreSimulator/Devices/80F3352C-678C-49E6-9FD7-07E126DF48D5/data/Containers/Data/Application/C1588187-6C72-435D-8A76-B2BB08B0F3EA/Documents/
Prasads-MacBook-Pro:Documents prasadkatankot$ ls
51fa7aae-bc12-49d7-9222-561d6352ae42.log results.json
Prasads-MacBook-Pro:Documents prasadkatankot$ tail -f results.json


No comments:

Post a Comment