Raymond Yeh
Raymond Yeh

Reputation: 285

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

Upvotes: 0

Views: 8259

Answers (2)

MkMan
MkMan

Reputation: 2191

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:

  1. Manipulate the variables inside the if test so they evaluate to true. This may require the use of spies depending on what's inside the if test.
  2. Inside an it block have something like expect(component.myFunction()).toBe(undefined);

Hope that helps.

Upvotes: 1

basarat
basarat

Reputation: 276199

How to test if () return; in a function

You don't using spy to determine the inside of the a function. 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.

If you want to know that a particular line return got tested, you need to use coverage reports.

Upvotes: 2

Related Questions