【问题标题】:Jasmine, karma, TypeError: Cannot read property 'returnValue' of undefinedJasmine,业力,TypeError:无法读取未定义的属性“returnValue”
【发布时间】:2020-12-29 05:32:09
【问题描述】:

我正在尝试使用 Jasmine 和 Karma 对组件进行单元测试。我正在使用 Angular 10。我正在测试的组件是 HomeComponent。我对检索适合初学者的课程进行了测试

import { filter } from 'rxjs/operators';
import {setupCourses} from '../common/setup-test-data';
import {
  async,
  ComponentFixture,
  TestBed,
} from '@angular/core/testing';
import { CoursesModule } from '../courses.module';
import { DebugElement } from '@angular/core';

import { HomeComponent } from './home.component';
import {
  HttpClientTestingModule,
} from '@angular/common/http/testing';
import { CoursesService } from '../services/courses.service';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { of } from 'rxjs';

describe('HomeComponent', () => {
  let fixture: ComponentFixture<HomeComponent>;
  let component: HomeComponent;
  let el: DebugElement;
  let courseService: any;

  const beginnerCourses = setupCourses().filter(course => course.category === 'BEGINNER');

  beforeEach(async(() => {
    const courseServiceSpy = jasmine.createSpyObj('CoursesService', [
      'findAllCourses',
    ]);

    TestBed.configureTestingModule({
      imports: [CoursesModule, HttpClientTestingModule, NoopAnimationsModule],
      providers: [{ provide: CoursesService, usevalue: courseServiceSpy }],
    })
      .compileComponents()
      .then(() => {
        fixture = TestBed.createComponent(HomeComponent);
        component = fixture.componentInstance;
        el = fixture.debugElement;
        courseService = TestBed.inject(CoursesService);
      });
  }));

  it('should create the component', () => {
    expect(component).toBeTruthy();
  });

  it('should display only beginner courses', () => {
    // pending();
     courseService.findAllCourses.and.returnValue(of(beginnerCourses));

     fixture.detectChanges();
  });

当然,我搜索了很多,但找不到任何有用的东西。

我仍然收到此错误:

TypeError: Cannot read property 'returnValue' of undefined
    at <Jasmine>
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/courses/home/home.component.spec.ts:51:39)
    at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:364:1)
    at ProxyZoneSpec.push../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/proxy.js:117:1)
    at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:363:1)
    at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:123:1)
    at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/jasmine-patch.js:176:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/jasmine-patch.js:191:1)
    at <Jasmine>

我不知道我必须改变什么? 谢谢。

【问题讨论】:

    标签: javascript angular unit-testing karma-jasmine


    【解决方案1】:

    您必须在beforeEach 块之外定义courseServiceSpy ,以便其余代码可以访问该变量。

    import { filter } from 'rxjs/operators';
    import {setupCourses} from '../common/setup-test-data';
    import {
      async,
      ComponentFixture,
      TestBed,
    } from '@angular/core/testing';
    import { CoursesModule } from '../courses.module';
    import { DebugElement } from '@angular/core';
    
    import { HomeComponent } from './home.component';
    import {
      HttpClientTestingModule,
    } from '@angular/common/http/testing';
    import { CoursesService } from '../services/courses.service';
    import { NoopAnimationsModule } from '@angular/platform-browser/animations';
    import { of } from 'rxjs';
    import SpyObj = jasmine.SpyObj;
    
    describe('HomeComponent', () => {
      let fixture: ComponentFixture<HomeComponent>;
      let component: HomeComponent;
      let el: DebugElement;
      let courseServiceSpy: SpyObj<CoursesService>; // Declare the spy here
    
      const beginnerCourses = setupCourses().filter(course => course.category === 'BEGINNER');
    
      beforeEach(async(() => {
        // Initialize the spy here
        courseServiceSpy = jasmine.createSpyObj('CoursesService', [
          'findAllCourses',
        ]);
    
        TestBed.configureTestingModule({
          imports: [CoursesModule, HttpClientTestingModule, NoopAnimationsModule],
          providers: [{provide: CoursesService, usevalue: courseServiceSpy}],
        })
          .compileComponents()
          .then(() => {
            fixture = TestBed.createComponent(HomeComponent);
            component = fixture.componentInstance;
            el = fixture.debugElement;
          });
      }));
    
      it('should create the component', () => {
        expect(component).toBeTruthy();
      });
    
      it('should display only beginner courses', () => {
        // pending();
        // Mock the spy here
        courseServiceSpy.findAllCourses.and.returnValue(of(beginnerCourses));
    
        fixture.detectChanges();
      });
    });
    

    在您之前的示例中,courseService 的定义仅在 beforeEach 块中“可见”。 Read up here 了解varletconst 的范围。

    【讨论】:

    • 当然谢谢。但我仍然得到同样的错误。
    • 请仔细看看我最后的编辑。如果错误仍然存​​在,请同时分享HomeComponent的代码。
    • 啊,是的,我明白了。谢谢!!没有注入
    猜你喜欢
    • 2018-07-27
    • 2017-06-01
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2021-10-31
    • 1970-01-01
    • 2018-07-24
    相关资源
    最近更新 更多