Angular 角度测试:测试时如何访问方法中的局部变量
我有一个方法,在里面我有一个条件,我想测试。比如:Angular 角度测试:测试时如何访问方法中的局部变量,angular,unit-testing,jasmine,Angular,Unit Testing,Jasmine,我有一个方法,在里面我有一个条件,我想测试。比如: myMethod():void { let a:boolean; a = this.myService.getValue(); if (a) { doThis(); if (!a) { doThat(); it("Should check if doThis called", () => { spyOn(myService, "getValue").andReturn(true);
myMethod():void {
let a:boolean;
a = this.myService.getValue();
if (a) {
doThis();
if (!a) {
doThat();
it("Should check if doThis called", () => {
spyOn(myService, "getValue").andReturn(true);
component.myMethod();
expect ...
});
因此,在我的it
中,我做了如下操作:
myMethod():void {
let a:boolean;
a = this.myService.getValue();
if (a) {
doThis();
if (!a) {
doThat();
it("Should check if doThis called", () => {
spyOn(myService, "getValue").andReturn(true);
component.myMethod();
expect ...
});
但是这个条件不起作用,
a
永远不会得到我与间谍嘲笑的“假”值
我的问题是,是否有任何方法可以访问/分配myMethod()
,a
的局部变量的值,或者唯一的方法是使其全局化,然后我可以在it
中执行类似组件的操作。a=
谢谢
let a;
spyOn(myService,"getValue").and.callFake(function(data){
a=data;
expect(data).not.toBeNull();
component.myMethod();
})
试试这可能会帮助你为什么不把这个.myService.getValue()作为myMethod的一个参数传递呢?可能是个主意,但是现在这个方法看起来像
a
永远不会得到我用spy模拟的“假”值??为什么呢?