我有一个简单的函数修改我的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