简体   繁体   English

角茉莉如何测试()是否返回; 在功能上

[英]Angular Jasmine How to test if () return; in a function

I intent to test a function as below: 我打算测试以下功能:

myFunction(input) {
  if (...) {
    return;
  } else {
    do something
  }
}

How to test the condition is return? 如何测试条件是否退货?

it('should', async(() => {
  spyOn(component, 'myFunction')
  // ????
}));

Thank you 谢谢

How to test if () return; 如何测试()是否返回; in a function 在功能上

You don't using spy to determine the inside of the a function. 您无需使用间谍来确定a函数的内部。 You spy on arguments and return values. 您监视参数并返回值。 You can use spy on to determine if the function returns undefined (the implicit value for return; ) for a given input. 您可以使用spy on来确定函数是否为给定的输入返回undefined的值( return;的隐式值)。

If you want to know that a particular line return got tested, you need to use coverage reports. 如果您想知道特定线路的退货已经过测试,则需要使用覆盖率报告。

Without actual code it's hard to write a code sample for you to use. 没有实际的代码,很难编写供您使用的代码示例。 If you want to test the return of the function of the if part the logic will be something like this: 如果要测试if部分功能的返回,则逻辑将是这样的:

  1. Manipulate the variables inside the if test so they evaluate to true. 在if测试中处理变量,以便它们评估为true。 This may require the use of spies depending on what's inside the if test. 这可能需要使用间谍程序,具体取决于if测试中的内容。
  2. Inside an it block have something like expect(component.myFunction()).toBe(undefined); 在一个it块中,有一个类似于expect(component.myFunction()).toBe(undefined);

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM