tonylukasavage / ti-mocha

Simple and reliable support for mocha testing with Appcelerator's Titanium SDK
Other
64 stars 21 forks source link

More detailed usage is needed. #24

Closed sg552 closed 9 years ago

sg552 commented 9 years ago

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

tonylukasavage commented 9 years ago

ti-mocha is for running tests in the Titanium runtime, meaning on a device or a simulator/emulator. Trying to run them via mocha at the CLI doesn't make sense in this context. You run the tests like you would run any Titanium app, like titanium build -p ios or via Titanium Studio.

  1. Yes. Put your tests where ever you want, preferably in the lib folder somewhere, there's no specific convention. It's your job to invoke the tests, so you can do it anywhere.
  2. Run titanium build to run the titanium app once you have your tests set up.
  3. ti-mocha is light weight and uses an existing and well-known testing API. There's no current plans for ti-mocha to replace drillbit.
sg552 commented 9 years ago

thanks Tony, I will check it out~

vklakshman1808 commented 7 years ago

@tonylukasavage , this is working for unit cases. I am trying use istanbul for code coverage for the same ti-mocha test cases. Could you help me in achieving this?

@sg552 @tonylukasavage If this is not achievable , please let me know any code coverage tools which uses ti-mocha test cases . Do you have any idea regarding this ?

Deepdhee11 commented 6 years ago

@sg552 can you please explain how to implement ti mocha in Alloy project..

sg552 commented 6 years ago

I am not using Titanium for 2 year. I am using native Android/iOS now. because native language is easier to debug ,to maintain , and for people in China, there're very few people knowing Titanium.

so, @Deepdhee11 I don't know how to use mocha in Alloy. @vklakshman1808 sorry I don't know. I almost forgot Titanium programming now... Orz

good luck guys, for people in China, we have only 2 options:

  1. use native programming language. ( easier to debug, easier to build team, easier to development )
  2. use wechat small program.