ti-unit
ti-unit copied to clipboard
How to mock getView() available in the controller
I am creating a new controller in controller.js: var newController=Alloy.createController('index').getView();
In test file (Controller_Spec.js) addded the following to mock getView var dealerControllerMock = { getView: function(){} }; beforeEach(function() { MockRequire = require('../../ti-unit/mockrequire'); MockRequire.addMock('getView', dealerControllerMock); controllerUnderTest = require('../../app/controllers/index'); });
but it is not working. I am getting the following error while running the npm test: TypeError: Cannot read property 'getView' of undefined
@Abi1210 Are you creating a new controller or a new widget? (on slack you mentioned widget, which is slightly different then creating a normal controller.
Besides that, in your test above you could mock Alloy by defining a global(thus not prefixed with var) mock, like eg. Alloy = { createController: funtion(){}}; In that case you even don't have to invoke MockRequire.addMock(..), also see the wiki https://github.com/aca-mobile/ti-unit/wiki/Testing-$,--Alloy.Globals,-Alloy.CFG-and-the-L-macro
I will update the documentation asap with some better examples.
@Abi1210 this must do the job. Can you confirm this please?
Alloy = {
createController: function(){}
};
var dealerControllerMock = {
getView: function(){}
}
it('my test', function(){
spyOn(Alloy, 'createController').andReturnValue(dealerControllerMock);
spyOn(dealerControllerMock, 'getView').andReturnValue(..);
...
expect(Alloy.createController).toHaveBeenCalledWith('dealer');
expect(dealerControllerMock.getView).toHaveBeenCalled();
});