Skip to content

Instantly share code, notes, and snippets.

@lbrenman
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbrenman/4a8c8ce651cc630f8543 to your computer and use it in GitHub Desktop.
Save lbrenman/4a8c8ce651cc630f8543 to your computer and use it in GitHub Desktop.
Appcelerator Titanium tijasmine simple titanium example
tijasmine Unit Test Demo
* Have a working project
- Note that i created a utils.js file in the lib folder with my business logic methods
e.g.
exports.concatName = function(a,b) {
return a.substring(0,1).concat(b);
}
* Download tijasmine from https://github.com/billdawson/tijasmine
* Download jasmine from https://github.com/pivotal/jasmine
- we will use 1.3.1 from the dist folder
* As per the tijasmine instructions copy jasmine.js (from jasmine/dist/{unzip 1.3.1}/lib) and tijasmine.js and tijasmine-console.js (from tijsamine/src) and place in your projects lib folder
* Create a specs folder under the apps folder for your jasmine unit tests (this is standard jasmine stuff)
* Add unit tests and note to add “require("tijasmine").infect(this);” at the top of your unit test file
e.g. specs/myspec.js:
require("tijasmine").infect(this);
var utils = require('utils');
describe("utilities", function() {
it("Concats two fields together", function() {
expect(utils.concatName("Leor","Brenman")).toEqual("LBrenman");
});
it("Concats two fields together", function() {
expect(utils.concatName("Leor","Brenman")).toEqual("lBrenman");
});
});
* In the test above, i am creating a passing test, followed by a failing test for demonstration purposes
* In your index.js, initiate a test using the following code:
var tijasmine = require("tijasmine"),
reporter = new (require("tijasmine-console").ConsoleReporter)();
tijasmine.addSpecModules("/specs/myspec");
tijasmine.addReporter(reporter);
tijasmine.execute();
* Launch your app on an iPhone device/simulator or Android device/emulator and review the console log, e.g.:
[INFO] : ------------------------------------------------------------
[INFO] : utilities:
[INFO] : - Concats two fields together (ok)
[INFO] : - Concats two fields together (FAILED)
[INFO] : - - Expected 'LBrenman' to equal 'lBrenman'.
[INFO] : ============================================================
[ERROR] : THERE WERE FAILURES!
[ERROR] : ============================================================
[ERROR] : Recap of failing specs:
[ERROR] : ------------------------------------------------------------
[ERROR] : utilities Concats two fields together. - Expected 'LBrenman' to equal 'lBrenman'.
[ERROR] : ------------------------------------------------------------
- Refer to the screen shot below for a typical session as described above:
* Refer to the following tutorials for other tips, tips and suggestions:
http://codingpie.com/2013/06/12/unit-testing-in-titanium-appcelerator-and-alloy-testing-commonjs-modules/
http://billdawson.com/an-appcelerator-titanium-jasmine-javascript-testing-module/
var utils = require("utils");
function doClick(e) {
$.sunameLBL.text = utils.concatName($.fnameTF.value, $.lnameTF.value);
}
$.index.open();
var tijasmine = require("tijasmine"),
reporter = new (require("tijasmine-console").ConsoleReporter)();
tijasmine.addSpecModules("/specs/myspec");
tijasmine.addReporter(reporter);
tijasmine.execute();
<Alloy>
<Window class="container" layout="vertical">
<!--Label id="label" onClick="doClick">Hello, World</Label-->
<Label id="fnameLBL">First Name</Label>
<TextField id="fnameTF"></TextField>
<Label id="lnameLBL">Last Name</Label>
<TextField id="lnameTF"></TextField>
<Button onClick="doClick" top="20">Submit</Button>
<Label id="unameLBL">Suggested Username</Label>
<Label id="sunameLBL"></Label>
</Window>
</Alloy>
require("tijasmine").infect(this);
var utils = require('utils');
describe("utilities", function() {
it("Concats two fields together", function() {
expect(utils.concatName("Leor","Brenman")).toEqual("LBrenman");
});
it("Concats two fields together", function() {
expect(utils.concatName("Leor","Brenman")).toEqual("lBrenman");
});
});
exports.concatName = function(a,b) {
return a.substring(0,1).concat(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment