Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 既然addProvider在RC6中被弃用,如何在angular2 jasmine测试规范中添加提供程序?_Angularjs_Angular_Karma Jasmine_Angular2 Testing - Fatal编程技术网

Angularjs 既然addProvider在RC6中被弃用,如何在angular2 jasmine测试规范中添加提供程序?

Angularjs 既然addProvider在RC6中被弃用,如何在angular2 jasmine测试规范中添加提供程序?,angularjs,angular,karma-jasmine,angular2-testing,Angularjs,Angular,Karma Jasmine,Angular2 Testing,我正试图这样在每个之前编写我的,以便每个都能获得所需的模块/提供程序: beforeEach(() => addProviders([ BaseRequestOptions, MockBackend, { provide: Http, useFactory: (backend: MockBackend, options: BaseRequestOptions) => new Http(ba

我正试图这样在每个之前编写我的
,以便每个
都能获得所需的模块/提供程序:

beforeEach(() => addProviders([
        BaseRequestOptions,
        MockBackend,
        {
            provide: Http,
            useFactory: (backend: MockBackend, options: BaseRequestOptions) => new Http(backend, options),
            deps: [MockBackend, BaseRequestOptions]
        },
        MyService
    ]));
但是,由于RC6中不推荐使用
addProviders
方法,那么添加我的提供程序的更好选择是什么?

使用from
@angular/core/testing
创建测试模块。比如说

beforeEach(() => {
  TestBed.configureTestingModule({
    imports: [ ... ],
    declarations: [ ... ],
    providers: [
      { .. },
      MyService
    ]
  });
});
它与配置常规模块几乎相同,只是不需要导出任何内容


请参阅中的更多完整示例。它有测试组件的示例,也使用了
测试床

除了接受的答案之外,我还需要添加以下内容以使测试工作在测试床上。compileComponents();我确实是这样做的,但即使我正在测试的服务在提供者列表中,它也会告诉我我的服务没有提供者(