Testing $, Alloy.Globals, Alloy.CFG and the L macro
davidcyp edited this page Jan 11, 2017
·
4 revisions
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');
});
We need to manually create mock implementations of Alloy.Globals and Alloy.CFG and put them in the global scope.