测试构成 OR 条件一部分的未覆盖分支

Test uncovered branch which forms part of OR condition

我想在我的代码库中测试这个未覆盖的分支。我附上了

所在位置的屏幕截图

测试如下:

  describe('addCourseContentCard()', () => {
    it('should add course content card', () => {
      spyOn(mockCourseContentService, 'onAddCourseContent').and.callThrough();
      let newCourseContents: ICourseContent[] = JSON.parse(JSON.stringify(courseContents));
      newCourseContents = (CourseContentService.addCourseContent(
        newCourseContents,
        CourseContentElementEditorService.setCourseContentEditorText(),
        newCourseContents[0].uid as string,
        CourseContentElementAddButtonEventType.AFTER
      ) as unknown) as ICourseContent[];
      newCourseContents = (CourseContentElementButtonService.resetCourseContentElementButtonNavigation(
        newCourseContents
      ) as unknown) as ICourseContent[];
      service.addCourseContentCard(course, newCourseContents, newCourseContents[0], 0);
      expect(mockCourseContentService.onAddCourseContent).toHaveBeenCalled();
    });

    it('should not add course content card', () => {
      spyOn(mockCourseContentService, 'onAddCourseContent').and.callThrough();
      let newCourseContents: ICourseContent[] = JSON.parse(JSON.stringify(courseContents));
      newCourseContents = (CourseContentService.addCourseContent(
        newCourseContents,
        CourseContentElementEditorService.setCourseContentEditorText(),
        newCourseContents[0].uid as string,
        CourseContentElementAddButtonEventType.AFTER
      ) as unknown) as ICourseContent[];
      newCourseContents = (CourseContentElementButtonService.resetCourseContentElementButtonNavigation(
        newCourseContents
      ) as unknown) as ICourseContent[];
      service.addCourseContentCard(null, null, newCourseContents[0], undefined);
      service.addCourseContentCard(null, null, newCourseContents[0], null);
      newCourseContents[newCourseContents.length - 1].uid = undefined;
      service.addCourseContentCard(course, newCourseContents, newCourseContents[0], 0);
      expect(mockCourseContentService.onAddCourseContent).not.toHaveBeenCalled();
    });
  });

我的代码如下:

  addCourseContentCard(
    course: ICourse | undefined | null,
    courseContents: ICourseContent[] | undefined | null,
    courseContentStep: ICourseContent,
    selectedCourseModule: number | undefined | null
  ): void {
    if (
      course &&
      courseContents &&
      (selectedCourseModule != undefined || selectedCourseModule != null)
    ) {
      let selectedCourseContentUid: string | undefined | null;
      if (courseContents[courseContents.length - 1].uid) {
        selectedCourseContentUid = courseContents[courseContents.length - 1].uid;
        courseContents = CourseContentService.addCourseContent(
          courseContents,
          courseContentStep,
          selectedCourseContentUid,
          CourseContentElementAddButtonEventType.AFTER
        );
        courseContents = CourseContentElementButtonService.resetCourseContentElementButtonNavigation(
          courseContents
        );
        this.courseContentService.onAddCourseContent(course, courseContents, selectedCourseModule);
      }
    }
  }

以下表达式的第二部分基本上是死代码,因此您无法编写覆盖它的单元测试。

(selectedCourseModule != undefined || selectedCourseModule != null)

如需进一步说明,请查看此answer