February 2019
Beginner
694 pages
18h 4m
English
Another benefit of Jasmine spies is that they can act as fakes. In other words, instead of calling a real function, the call is temporarily overridden to call a fake function. These fake functions can also return values—which can be very useful for generating small mocking frameworks. Consider the following test:
class ClassToFake {
getValue() : number {
return 2;
}
}
describe("using fakes", () => {
it("calls fake instead of real function", () => {
let classToFake = new ClassToFake();
spyOn(classToFake, 'getValue').and.callFake ( () => {
return 5;
});
expect(classToFake.getValue()).toBe(5);
});
});
We start with a class named ClassToFake, which has a single function, getValue, which returns 2. Our test then creates an ...
Read now
Unlock full access