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';

    // 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? Dec 24, 2018 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, 2018 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) Dec 25, 2018 at 12:48


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.