Testing $, Alloy.Globals, Alloy.CFG and the L macro - aca-mobile/ti-unit GitHub Wiki

L

We don't spy on L directly (it's a macro), instead we provide a mock implementation which returns our key. Now we can create a simple expectation on the surrounding function!

L = function(s){
   return s;
};

function _onAuthLoginError() {
    Alloy.Globals.notifications.showError(L(‘my.message’));
}
 
it('should show error message when a login error has occured', function(){
    spyOn(Alloy.Globals.notifications, 'showError');
    controllerUnderTest.test._onAuthLoginError();
    expect(Alloy.Globals.notifications.showError).toHaveBeenCalledWith(‘my.message’);
});

$

Simply call #createControllerMock with the path to your controller, and assign this to a global variable $.

beforeEach(function () {
   // generate the mock
   $ = require('tiunit/mockcontroller').createControllerMock('./app/controllers/myController.js');

   MockRequire.addMock('user.manager', userManagerMock);
   controllerUnderTest = require('../app/controllers/authenticate');
});

Alloy.Globals, Alloy.CFG

We need to manually create mock implementations of Alloy.Globals and Alloy.CFG and put them in the global scope.