javascript 错误:需要先调用TestBed.initTestEnvironment()

olhwl3o2  于 2022-12-25  发布在  Java
关注(0)|答案(4)|浏览(157)

我在试着做一个发球Angular 的测试。
这是我的密码

describe('AddressService', () => {

  let service: AddressService;
  let injector: TestBed;
  let httpTestingController: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [AddressService]
    });

    injector = getTestBed();
    service = injector.inject(AddressService);
    httpTestingController = injector.inject(HttpTestingController);
    
    // service = TestBed.inject(AddressService);
    

  });

  afterEach(() => {
    httpTestingController.verify();
  })

  httpTestingController = TestBed.inject(HttpTestingController);

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  const dummyAddressListResponse = {
    data: [
      {direccion: 'address1'}, {Colas: 'queue1'},
      {direccion: 'address2'}, {Colas: 'queue2'}
    ],
  };

  it('getAddress() should return data', () => {
    service.getAddress().subscribe((res) => {
      expect(res).toEqual(dummyAddressListResponse);
    });

    const req = httpTestingController.expectOne(`${environment.URI}/mock-address`);
    expect(req.request.method).toBe('GET');
    req.flush(dummyAddressListResponse);
  })

});

在运行测试时ng test --main源代码/应用程序/服务/地址/地址.服务.规范.ts
我看到此错误错误:需要先调用TestBed.initTestEnvironment()
我已经找过了,没有找到任何解决办法,它发生在某人身上吗?

ubby3x7f

ubby3x7f1#

对于jest用户-只需在setup-jest.js中添加以下代码。因为jest需要初始化。

import { TestBed } from "@angular/core/testing";
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from "@angular/platform-browser-dynamic/testing";

TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
zpf6vheq

zpf6vheq2#

第一件事不应使用--main,它指向入口点,而不是所需的测试,并且应为src/test.ts
要运行单个测试,请使用以下命令:
ng测试-包括“应用程序/服务/地址/地址.服务.规范.ts”
测试应该有一点不同:

describe('AddressService', () => {
  let service: AddressService;
  let injector: TestBed;
  let httpTestingController: HttpTestingController;

  beforeEach(async () => {
    // let's compile TestBed first.
    await TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [AddressService],
    }).compileComponents();

    // let's use TestBed.injector.
    service = TestBed.inject(AddressService);
    httpTestingController = TestBed.inject(HttpTestingController);
  });

  afterEach(() => {
    httpTestingController.verify();
  })

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('getAddress() should return data', () => {
    const dummyAddressListResponse = {
      data: [
        {direccion: 'address1'}, {Colas: 'queue1'},
        {direccion: 'address2'}, {Colas: 'queue2'}
      ],
    };

    let actual: any;
    service.getAddress().subscribe((res) => actual = res);

    const req = httpTestingController.expectOne(`${environment.URI}/mock-address`);
    expect(req.request.method).toBe('GET');
    req.flush(dummyAddressListResponse);

    expect(actual).toEqual(dummyAddressListResponse);
  });
});
noj0wjuj

noj0wjuj3#

问题是它必须运行从test.ts开始的所有内容。
因此,只需将const context = require.context('./', true, /\.spec\.ts$/);更改为const context = require.context('./', true, /address\.service\.spec\.ts$/);并使用ng test命令,而不是运行ng test --main src/app/services/address/address.service.spec.ts命令。

dced5bon

dced5bon4#

这是否使用ng-packagr(即Angular 库)?如果是,您可能需要检查./project/文件夹下是否没有node_modules。
这给了我一个完全相同的错误。当我删除项目文件夹下的node_modules时,它又开始工作了。
来源:https://github.com/ngneat/spectator/issues/546

相关问题