我有一個簡單的函數修改我的app.component.ts中的參數,我想用間諜測試函數。我的changeText函數總是未定義出於某種原因,我在做什麼錯?角4茉莉間諜單元測試「預計未定義爲'新文本'
AppComponent.ts
export class AppComponent {
text = "My text";
changeText = function() {
this.text = "New text";
return this.text;
}
}
AppComponent.spec.ts
describe("my text with Spies", function() {
it("should be altered", 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"); //Fails
expect(app.changeText).toHaveBeenCalledTimes(1);
});
});
請告訴我的錯誤? – onetwo12
規範失敗,錯誤「預期未定義爲'新文本'因此,changeText的輸出由於某種原因未定義。 – kikilolohu
也許引用此:https://stackoverflow.com/questions/35987055/how-to-write-unit -testing-for-angular-2-typecript-for-private-methods-with-ja?rq = 1可能是一個範圍界定問題 – rcheuk