因此,在版本RC5 of Angular2中,他们反对HTTP_PROVIDERS并引入了HttpModule。对于我的应用程序代码,这是很好的工作,但我正在努力改变我的茉莉花测试。
下面是我目前在我的规范中所做的事情,但是既然HTTP_PROVIDERS被废弃了,我现在应该做什么呢?有什么东西我需要提供而不是HTTP_PROVIDERS?在RC5世界中,正确的方法是什么?
beforeEach(() => {
reflectiveInjector = ReflectiveInjector.resolveAndCreate([
HTTP_PROVIDERS,
...
]);
//other code here...
});
it("should....", () => { ... });发布于 2016-08-25 15:49:22
现在不推荐的HTTP_PROVIDERS被替换为HttpModule is RC5。
在描述块中,使用必要的导入和提供程序数组属性添加TestBed.configureTestingModule,如下所示:
describe("test description", () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [SomeService]
});
});
it("expect something..", () => {
// some expectation here
...
})
})这就是我如何让我的单元服务测试与RC5一起工作的方式,希望这不会在下一个版本候选版本和最终版本之间发生变化。如果你和我一样,你可能会对发布候选人之间的反对意见感到沮丧。我希望事情能很快稳定下来!
发布于 2016-09-12 09:27:55
当从预Rc5代码更新到RC6时,我遇到了类似的问题。为了扩展Joe W上面的答案,我替换了以下代码:
import { ReflectiveInjector, provide } from '@angular/core';
import { HTTP_PROVIDERS, RequestOptions } from '@angular/http';
export function main() {
describe('My Test', () => {
let myService: MyService;
beforeAll(() => {
let injector = ReflectiveInjector.resolveAndCreate([
HTTP_PROVIDERS,
provide(RequestOptions, { useValue: getRequestOptions() }),
MyService
]);
myService = injector.get(MyService);
});
it('should be instantiated by the injector', () => {
expect(myService).toBeDefined();
});
...使用这个RC6代码(我猜它也适用于RC5):
import { TestBed } from '@angular/core/testing';
import { HttpModule, RequestOptions } from '@angular/http';
export function main() {
describe('My Test', () => {
let myService: MyService;
beforeAll(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
{ provide: RequestOptions, useValue: getRequestOptions() },
MyService
]
});
myService = TestBed.get(MyService);
});
it('should be instantiated by the testbed', () => {
expect(myService).toBeDefined();
});
...https://stackoverflow.com/questions/38903607
复制相似问题