角度测试:如何在测试时访问方法中的局部变量



>我有一个方法,里面有一个我想测试的条件。像这样:

myMethod():void {
  let a:boolean;
  a = this.myService.getValue();
  if (a) { 
    doThis();
  if (!a) { 
    doThat();

所以在我的it中,我做了这样的事情:

it("Should check if doThis called", () => {
     spyOn(myService, "getValue").andReturn(true);
     component.myMethod();
     expect ...
});

但是这种情况不起作用,a永远不会得到我用间谍嘲笑的"假"值

我的问题是,是否有任何方法可以访问/为myMethod()a 的局部变量分配值,或者唯一的方法是使其全局化,然后我可以在it中做类似component.a =

谢谢。

let a;
spyOn(myService,"getValue").and.callFake(function(data){
      a=data;
      expect(data).not.toBeNull();
component.myMethod();
    })

试试这个可能会对你有所帮助

最新更新