Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mocha testing in Titanium(ja)

Mocha testing in Titanium(ja)

Kosuke Isobe

August 03, 2015
Tweet

More Decks by Kosuke Isobe

Other Decks in Programming

Transcript

  1. ͜Ε͘Β͍ͳΒ w KTIJOU w KTCFBVUJpFS w ςετ͡Όͳͯ͘จ ๏νΣοΫͰ͢Ͷ // gulplfile.js

    var gulp = require(‘gulp’), plugin = require(‘gulp—load—plugins’)(); gulp.task(‘jshint’, function(){ return gulp.src(‘app/controllers/**/ *.js’) .pipe(plugin.jshint()) .pipe(plugin.jshint.reporter(‘jshint— stylish’)) .pipe(plugin.jshint.reporter(‘fail’)); }); gulp.task(‘jsbeautifier’, function(){ return gulp.src(‘app/controllers/**/ *.js’) .pipe(plugin.jsbeautifier({ logSuccess: false, mode: ‘VERIFY_ONLY’ })); });
  2. VM Ϟδϡʔϧ w ͜Μͳײ͡Ͱ࣮ߦ؀ڥ Λ࡞ͬͯ͋͛·͢ w WNSVO*O$POUFYUͷ ฦ٫஋͸࣮ߦ݁Ռͷί ϯςΩετ˺Ξαʔ γϣϯͰ͖Δ͡Όͳ͍

    var vm = require(‘vm’), context = vm.createContext({ require: require, module: module, exports: exports, console: console, setTimeout: setTimeout, setInterval: setInterval, clearTimeout: clearTimeout, clearInterval: clearInterval, JSON: JSON, alert: function(e) { console.log(e); } }), result = vm.runInContext(‘console.log(\’ Hello, world.\’);’, context);
  3. VM Ϟδϡʔϧ w WNDSFBUF$POUFYUʹ 5JUBOJVN"1*Λ౉ͯ͠ ͋͛Ε͹࣮ߦͰ͖Δʁ w Ͳ͏ߟ͑ͯ΋ಈ͖·͢ ΑͶɺ͜ΕɻΑ͠ɺ5Jʙ ͳ"1*શ෦ॻ͔͘ʜ

    var vm = require(‘vm’), context = vm.createContext({ Ti: { UI: { createWindow: function(){} } } }), result = vm.runInContext(‘var win = Ti.UI.createWindow();’, context);
  4. Mocha testing in ti-slag w ඞཁͳOQNύοέʔ δΛΠϯετʔϧ w QBDLBHFKTPO΁ς ετͷ࣮ߦεΫϦϓ

    τΛ௥Ճ $ cd path/to/yourapp $ npm init $ npm install mocha ti—slag lodash ——save—dev package.json { … “scripts”: { “test”: “mocha test.js” }, … }
  5. Mocha testing in ti-slag w ·ͣ͸"MMPZίϯύΠ ϧ͠·͢ɻUJCVJMEʗ BQQDSVOͱ͔Ͱ΋0,ɻ ͱʹ͔͘3FTPVSDF഑ ԼʹίʔυΛు͔ͤ·͢

    w ͦͯ͠OQNUFTU $ alloy compile ——config platform=ios $ npm test > [email protected] test /Users/ Kosuke/src/sandbox > mocha test.js iOS index.js ✓ should does not throw exception in the Controller 1 passing (10ms)
  6. // Resources/…/index.js function Controller() { function doClick() { var next

    = Alloy.createController(‘next’ ), view = next.getView(); view.open(); } $.index.open(); } module.exports = Controller; // app/controllers/index.js function doClick() { var next = Alloy.createController(‘next’) , view = next.getView(); view.open(); } $.index.open(); Mocha testing in ti-slag
  7. Mocha testing in ti-slag // test.js var _ = require(‘lodash’),

    assert = require(‘assert’), path = require(‘path’), slag = require(‘ti—slag); describe(‘iOS index.js’, function(){ var context = slag(‘./Resources/iphone/alloy/controllers/index.js’, { titanium: ‘4.0.0.GA’, platfrom: ‘ios’, module: { alloy: alloy, ‘alloy/controllers/BaseController’: BaseController } }); it(‘should does not throw exception in the Controller’, function(){ assert.doesNotThrow(function(){ context.Controller(); }); }); });
  8. Mocha testing in ti-slag w ୈೋҾ਺ʹ͋ΔNPEVMF Ͱϐϯͱ͖͔ͨ΋͠Ε· ͤΜͶ w SFRVJSF͢ΔϞδϡʔϧ

    ͷ໊শΛ౉ͤ͹ͦͷ·· ಡΈࠐ·ΕΔͷͰɺ͓޷ ͖ʹͲ͏ͧ var context = slag(‘./ Resources/iphone/alloy/ controllers/index.js’, { titanium: ‘4.0.0.GA’, platfrom: ‘ios’, module: { ‘ti.map’: { // ࣗ෼Ͱ͕Μ͹Ε } } });
  9. Mocha testing in ti-slag w UJTMBH಺ʹ"MMPZίϯςΩ ετ͕ల։͞Ε͍ͯΔͷͰɺ "MMPZίϯτϩʔϥͷϝιο υ΋ͦͷ··࢖͑Δ w

    "MMPZ!͔Β௥Ճ͞Ε ͨΠϕϯτ௥Ճʗऔಘʗ࡟আ ϝιουͰΠϕϯτϦεφΛ औಘ͢Ε͹0, assert.doesNotThrow(function(){ context.Controller(); var listeners = context.getListener(); });
  10. Mocha testing in ti-slag describe(‘iOS index.js’, function(){ var context =

    slag(‘./Resources/iphone/alloy/controllers/index.js’, { titanium: ‘4.0.0.GA’, platfrom: ‘ios’, module: { alloy: alloy, ‘alloy/controllers/BaseController’: BaseController } }), listeners; it(‘should does not throw exception in the Controller’, function(){ assert.doesNotThrow(function(){ context.Controller(); listeners = context.getListener(); }); }); it(‘should does not throw exception in the event listeners’, function(){ assert.doesNotThrow(function(){ _.each(listeners, function(listener){ listener.handler(); }); }); }); });
  11. circle.yml w UFTUQSFͰHVMQ KTIJOUKTCFBVUJpFS ͔ͯ͠Β"MMPZίϯ ύΠϧ w ͦͷίϯύΠϧ݁ՌΛ ࣋ͬͯςετΛ࣮ߦ machine:

    node: version: 0.10.34 dependencies: pre: — npm install —g alloy — npm install test: pre: — gulp jshint — gulp jsbeautifier — alloy compile ——config platform=ios — alloy compile ——config platform=android