Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度测试:测试时如何访问方法中的局部变量_Angular_Unit Testing_Jasmine - Fatal编程技术网

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模拟的“假”值??为什么呢?