Back

关于Ti-mocha , 我的疑问

发布时间: 2014-12-25 07:39:00

refer to:  https://github.com/tonylukasavage/ti-mocha/issues/24

hi Tony,

My team is using Titanium SDK 3.5, now it's time for me to add unit tests to it. However, I am not able to run the "Basic Usage" in the document, nor the "Titanium + mocha + should example".

## For the Basic Usage example, I did the following steps:
step1.1. $ npm install ti-mocha ( after that, app/lib/ti-mocha.js appears)
step1.2. $ touch my_test.js , add content to it,

```
1 // creates the "mocha" global necessary to run a test suite anywhere in your app
2 require('ti-mocha');
3
4 // create the test suite
5 describe('ti-mocha', function() {
6
7 describe('suite 1', function() {
8
9 it('shows passing tests (fast)', function(){});
10
11 it('shows passing tests (slow)', function(done){
12 setTimeout(done, 1500);
13 });
14
15 });
16
17 describe('suite 2', function() {
18
19 it('shows pending tests');
20
21 it('fails a test', function() {
22 throw new Error('this shoud fail');
23 });
24
25 });
26
27 });
28
29 // run the tests
30 mocha.run();
```
setp1.3. then `$ mocha my_test.js` , error appeard:

```
Ti.API.log(type === 'log' ? 'info' : type, util.format.apply(this, args));
^
ReferenceError: Ti is not defined
```

## So I head for the more detailed example: Titanium + mocha + should example, also I met problems:

step2.1. I create a file named 'app.js' in the root of my project folder. content is:

```
require('ti-mocha');

// create a basic UI
var win = Ti.UI.createWindow({
backgroundColor: '#fff',
fullscreen: false,
exitOnClose: true,
id: 'myWindow'
});
var view = Ti.UI.createView({
height: Ti.UI.FILL,
width: Ti.UI.FILL,
backgroundColor: '#a00',
id: 'myView'
});
win.add(view);

// run tests after window opens to ensure UI is initialized
win.addEventListener('open', function() {
require('test/app_test')(win, view);
});

// show the UI
win.open();
```

step2.2. I created a test file in: test/test_app.js:

```
var should = require('should');

module.exports = function(win, view) {

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

describe('#myWindow', function() {

it('exists', function() {
should.exist(win);
win.id.should.equal('myWindow');
});

it('has Ti.UI.Window functions', function() {
should(win.open).be.a.Function;
should(win.close).be.a.Function;

if (Ti.Platform.name === 'iPhone OS') {
should(win.hideTabBar).be.a.Function;
}
});

it('has dimensions equal to the device', function() {
win.size.height.should.equal(Ti.Platform.displayCaps.platformHeight);
win.size.width.should.equal(Ti.Platform.displayCaps.platformWidth);
});

});

describe('#myView', function() {

it('exists', function(){
should.exist(view);
view.id.should.equal('myView');
});

it('has Ti.UI.View functions', function() {
should(view.add).be.a.Function;
});

it('is a child of window', function() {
win.children.length.should.equal(1);
should.exist(win.children[0]);
win.children[0].id.should.equal('myView');
});

it('view has same dimensions as window', function(){
view.size.height.should.equal(win.size.height);
view.size.width.should.equal(win.size.width);
});

});

});

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

step2.3 then I run: $ mocha ( in the root of my project folder )

```
$ mocha

0 passing (3ms)
```

Neither of the example runs as my expect. I think I must miss something.

My question is:

1. is ti-mocha support Alloy? what is the app.js used for? where should I put it ? My project is an Alloy project. the controllers are all in app/controller folder.
2. where and which command should I run? `$ cd <my_titanium_project> && mocha` ? or something else?
3. I searched the official Titanium document ( 3.x version) , only found a Post about "Drillbit:(http://docs.appcelerator.com/titanium/3.0/#!/guide/Writing_Unit_Tests_with_Drillbit) ". It seems that you are in the titanium Organization, what's your point of Ti-mocha v.s. Drillbit? will Ti-mocha replace Drillbit in the titanium documentation?

thanks
Siwei

Back