using Jasmines spyon upon a private method
is it possible to use Jasmine unit testing framework's spyon method upon a classes private methods?
The documentation gives this example but can this be flexivble for a private function?
describe("Person", function() {
it("calls the sayHello() function", function() {
var fakePerson = new Person();
spyOn(fakePerson, "sayHello");
fakePerson.helloSomeone("world");
expect(fakePerson.sayHello).toHaveBeenCalled();
});
});
Solution 1:
Just add a generic parameter < any> to the spyon() function:
spyOn<any>(fakePerson, 'sayHello');
It works on perfectly !
Solution 2:
spyOn<any>(fakePerson, 'sayHello');
expect(fakePerson['sayHello']).toHaveBeenCalled();
by adding <any>
to spyOn you remove it from typescript type check.
you also need to use array index notation in order to access a private method (sayHello) in the test expect