Skip to content

Instantly share code, notes, and snippets.

@lbrenman
Last active April 19, 2017 22:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lbrenman/0c18239184cec1c8c74b to your computer and use it in GitHub Desktop.
Save lbrenman/0c18239184cec1c8c74b to your computer and use it in GitHub Desktop.
Appcelerator Titanium ti-mocha simple titanium example
Titanium has no built in Unit Test as Xcode/ObjC and Android Java have
Jasmine and Ti-mocha are the two main unit test frameworks for titanium
Jasmine runs without requiring the app to run
Ti-mocha runs while the app is running
Using T-Mocha
References:
https://github.com/tonylukasavage/ti-mocha
http://tonylukasavage.com/ti-mocha/
https://github.com/rblalock/core
* Add ti-mocha.js to app/lib folder
* add tests folder to app/lib folder to hold unit tests
* add test js file to tests folder (e.g. mytests.js)
* require mytests,js in alloy.js:
- require(‘tests/mytests)
* add ti-mocha unit tests to mytests.js
- in below example, we are testing business logic (add2nums()) and a controller (makeRow.js)
require('ti-mocha');
mocha.setup({ reporter: 'ti-spec-studio' });
var alloy = require('alloy');
var utils = require('utils');
// create the test suite
describe('Leors Unit Test', function() {
describe('utilities', function() {
it('Adding two numbers together', function(){
var results = utils.add2nums(11,22);
if(results !== 33) {
throw new ("Add Two Numbers Together FAILED");
}
});
it('Verify row controller', function(){
var controller = alloy.createController('makeRow', {
name: "uniqueName",
});
if(controller.makeLbl.text !== "uniqueName"){
throw new ("Verify row controller FAILED");
}
});
});
});
// run the tests
mocha.run();
* In example above, utils.js in the lib folder contains the methods to be tested, utils.js:
exports.add2nums = function(a,b) {
return a + b;
}
* See corejs for example on how to configure ti-mocha from config.js
* Just run the app in Studio onto iPhone sim or device or Android emulator or device and see ti-mocha results in console can also see in CLI
require('tests/mytests');
$.makeTV.addEventListener('click', function(e) {
Ti.API.info("index: $.makeTV.addEventListener(click)");
});
$.makeBTN.addEventListener('click', function(e){
Ti.API.info("index: $.makeBTN.addEventListener(change)");
getMakes({
success: function(e) {
Ti.API.info('Received data = '+e);
loadTable(e);
},
error: function(e) {
Ti.API.info('Error = '+e);
alert("No network or server not available. Please try again.");
}
});
});
function loadTable(e) {
Ti.API.info("index: loadTable()");
var reply = JSON.parse(e);
var rows = [];
var i = 0;
Ti.API.info("index: reply = "+JSON.stringify(reply));
Ti.API.info("index: reply.makes.length = "+reply.makes.length);
if(reply.makes.length>0){
_.each(reply.makes, function(item) {
rows.push(Alloy.createController('makeRow', {
name: item.name,
//year: item.year,
}).getView());
});
}
else {
alert("No makes found.");
}
$.makeTV.setData(rows);
}
function getMakes(o){
Ti.API.info("getMakes()");
if(Titanium.Network.networkType == Titanium.Network.NETWORK_NONE){
Ti.API.info("index: getMakes() - No Network");
if (o.error) { o.error("No Network"); };
return;
}
var xhr = Titanium.Network.createHTTPClient({
onload: function() {
if (o.success) { o.success(this.responseText); };
},
onerror: function(e) {
if (o.error) { o.error("Error with Edmunds API"); };
},
timeout: 10000,
});
xhr.open("GET", "https://api.edmunds.com/api/vehicle/v2/makes?state=new&fmt=json&api_key=97zchuauxdqzw5thqxqhpzgq");
xhr.send();
};
$.index.open();
<Alloy>
<Window class="container" layout="vertical">
<View height="40" backgroundColor="black">
<Label color="white">Web Service Demo</Label>
</View>
<View layout="vertical">
<Button id="makeBTN" top="20">Get Makes</Button>
<TableView id="makeTV" top="20"/>
</View>
</Window>
</Alloy>
var args = arguments[0] || {};
//Ti.API.info("row created, args.name = "+args.name);
$.makeRow.name = args.name;
//$.movieRow.year = args.year;
$.makeLbl.text = args.name;
//$.yearLbl.text = args.year;
<Alloy>
<TableViewRow class="movieTVR">
<Label id="makeLbl" width="80%" left="10"/>
<!--Label id="yearLbl" width="20%" right="0"/-->
</TableViewRow>
</Alloy>
require('ti-mocha');
//mocha.setup({ reporter: 'ti-spec-studio' });
var alloy = require('alloy');
var utils = require('utils');
// create the test suite
describe('Leors Unit Test', function() {
describe('utilities', function() {
it('Adding two numbers together', function(){
var results = utils.add2nums(11,22);
if(results !== 33) {
throw new ("Add Two Numbers Together FAILED");
}
});
it('Verify row controller', function(){
var controller = alloy.createController('makeRow', {
name: "uniqueName",
});
if(controller.makeLbl.text !== "uniqueName"){
throw new ("Verify row controller FAILED");
}
});
});
});
// run the tests
mocha.run();
exports.add2nums = function(a,b) {
return a + b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment