I have a simple function in my app.component.ts that is meant to modify a parameter, and I am trying to test this function using a spy. However, for some reason, my changeText function always returns undefined. Can you help me identify what I might be doing wrong?
Here is the code snippet from AppComponent.ts:
export class AppComponent {
text = "My text";
changeText = function () {
this.text = "New text";
return this.text;
}
}
And here is the testing code from AppComponent.spec.ts:
describe("Testing with Spies", function () {
it("should successfully alter the text", function () {
const fixture = TestBed.createComponent(AppComponent);
const app = this.fixture.debugElement.componentInstance;
spyOn(app, 'changeText');
expect(app.text).toBe("My text");
expect(app.changeText()).toBe("New text"); //This test fails
expect(app.changeText).toHaveBeenCalledTimes(1);
});
});