【问题标题】:Angular 2 beforeEachProviders fail with Injector in testsAngular 2 beforeEach Providers 在测试中因 Injector 而失败
【发布时间】:2023-04-04 10:07:01
【问题描述】:

当我尝试在测试中将Injector 传递给beforeEachProviders 时,出现以下错误。

Failed: Cannot resolve all parameters for 'Injector'(?, ?, ?, ?, ?).
Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Injector' is decorated with Injectable.

我使用的代码

import { Injector } from 'angular2/core';

describe(() => {
  beforeEachProviders(() => [Injector]);
});

缺少什么?我应该给提供者什么才能实例化它?

【问题讨论】:

  • 如果不看你的代码是什么样子就很难判断。

标签: testing angular angular2-testing


【解决方案1】:

我认为您不需要在提供程序中配置 Injector 以进行测试。可以将此类的实例直接注入到您的测试中...

这是我的一个测试中的内容(Injector 没有“显式”提供程序),我可以稍后将其注入测试:

import {it, describe, expect, beforeEach, inject, beforeEachProviders} from 'angular2/testing';
import {HTTP_PROVIDERS, XHRBackend, Response, ResponseOptions} from 'angular2/http';
import {MockBackend, MockConnection} from 'angular2/http/testing';
import {provide, Injector} from 'angular2/core';
import {HttpService} from "./http-service";

describe('HttpService Tests', () => {
  beforeEachProviders(() => {
    return [
      HTTP_PROVIDERS,
      provide(XHRBackend, { useClass: MockBackend }),
      HttpService
    ];
  });

  it('Should return a list of dogs', inject(
         [XHRBackend, HttpService, Injector],
         (mockBackend, httpService, injector) => {
    console.log(injector); // <----- Not null
  });
});

【讨论】:

  • 是的,问题是我想在 beforeEachProviders 中添加它,删除后它对我也很好。
猜你喜欢
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多