I have my Angular Factory this way:
angular
.module('app.myModule')
.factory('myFactory', myFactory);
myFactory.$inject = [];
function myFactory() {
var factory = {
func1: func1,
func2: func2
};
return factory;
//////////
function func1() {
if (/* some condition */) {
return true;
} else {
return false;
}
};
function func2() {
if (func1()) {
// do something
} else {
// do something else
}
};
};
func2()
will do something depending on the result of calling func1
, but func1()
is not a private function because it's interesting for me to have it exposed.
Everything was fine, until I started to code this factory's unit tests. When testing func2()
, I want to stub func1()
just to return true
/false
whenever I want.
So far I coded a test:
describe('Test func2 if func1 returns false', function () {
beforeEach(function () {
// func1 stub setup
sinon.stub(myFactory, 'func1');
myFactory.func1.returns(false);
// HERE myFactory.func1() RETURNS 'false'
});
it('should do something else', function (done) {
// HERE func1() CALLED INSIDE myFactory.func2() IS NOT THE STUB
myFactory.func2();
// Do some assertions, etc.
});
});
but I noticed that if I did a small change in myFactory's func2()
code by calling this.func1()
instead of func1()
like this:
function func2() {
if (this.func1()) {
// do something
} else {
// do something else
}
};
from that point on, the stubbed func1()
function always gets called in the tests.
At this point I'm just confused. Does anyone know what might be going on here? Would it be more "correct" to call any other public method/function inside my angular factories with this.
if it belongs to the same factory?
Aucun commentaire:
Enregistrer un commentaire