1

I'm writing some unit tests for our Angular application. In a few instances, we have some code that is like the following.

public submit(): void 
{
    if (!this.selectedValue) 
      {
          this.error = 'some error';
          return;
      }

    // more function things
}

Is there a way to test that the function returns when calling submit() and there is no selectedValue?

  • Any problem with setting the selectedValue attribute and retrieving the error attribute on the test? – João Farias Dec 24 '18 at 11:17
  • @JoãoFarias that’s what we have now basically. ‘ng test -code-coverage’ likes to show that the return line isn’t covered though. We weren’t sure if you could test just the return. – DrZoo Dec 24 '18 at 19:46
  • Javascript funcions return Undefined if nothing is specified. You can assert that and the change in the error attribute. I wouldn't say that the code coverage is only partial useful here - I would focus more on the changes aasertion. (OBS: If it answer you, let me know, so I can create a proper answer entry) – João Farias Dec 25 '18 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.