简体   繁体   中英

Angular Service Unit Testing - faking nested methods or not?

I have very simple question.

If i have in one service methods like this one

generateObjectBasedOnName(name: string): SomeObject {
  if(this.isNameValid(name)) {
    const {namePart1, namePart2} = this.getNameParts(name);
    const baseObjectOne = this.getBaseObjectOne(namePart1);
    const baseObjectTwo = this.getBaseObjectTwo(namePart2);

    baseObjectOne.someDataNeededToSetHere = false;
    baseObjectTwo.someDataNeededToSetHere = true;

    return this.generateSomeObject(baseObjectOne, baseObjectTwo);
  }

  return null;
}

Should I, in unit tests, fake methods that are used in this method or should I just leave them to execute normally?

Thanks for any answers :)

The purpose of a unit test is to validate that a specific software unit (in your case that's your service class) performs as designed. Therefore...

  • you must not mock individual methods of the unit under test.
  • you should possibly mock methods of classes that are injected to the unit under test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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