【发布时间】:2017-06-02 18:06:16
【问题描述】:
首先对不起我的英语。
我正在使用 Angular 并通过他的测试开发一个简单的服务。
该服务使用 http.get 方法加载一个 JSON 文件并将其内容存储在一个变量中。 目标并不重要。
在有效的代码下方:
首先是服务
import {Http} from '@angular/http';
import {Injectable} from '@angular/core';
import {Constants} from 'config/constants';
import 'rxjs/Rx';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class LoadJsonFileService {
private config: Object = null;
constructor(private http: Http) {
}
private loadJsonAndUpdateConfig() {
this.http
.get(Constants.CONFIG_FILE_NAME)
.map(response => response.json())
.catch((error: any): any => {
return Observable.throw(error.json().error || 'Server error');
}).subscribe((configJson) => {
this.config = configJson;
});
}
public getConfig(key: string): string {
if (!this.config) {
this.loadJsonAndUpdateConfig();
}
return this.config[key];
}
}
然后是测试:
import {BaseRequestOptions, ConnectionBackend, Http, RequestOptions, Response, ResponseOptions} from '@angular/http';
import {MockBackend, MockConnection} from '@angular/http/testing';
import {LoadJsonFileService} from './load-json-file.service';
import {inject, TestBed} from '@angular/core/testing';
describe('Test suite ...', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{provide: RequestOptions, useClass: BaseRequestOptions},
{provide: ConnectionBackend, useClass: MockBackend},
Http,
LoadJsonFileService
]
});
});
it('The test ...', inject([ConnectionBackend, LoadJsonFileService],
(backend,
service: LoadJsonFileService) => {
backend.connections.subscribe((c: MockConnection) => {
c.mockRespond(new Response(new ResponseOptions({body: {key: 'foo'}})));
});
expect(service.getConfig('key')).toBe('foo');
}));
});
测试正常。
代码不起作用,我不知道为什么:
import {Http} from '@angular/http';
import {Injectable} from '@angular/core';
import {Constants} from 'config/constants';
import 'rxjs/Rx';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class LoadJsonFileService {
private config: Object = null;
constructor(private http: Http) {
this.loadJsonAndUpdateConfig();
}
private loadJsonAndUpdateConfig() {
this.http
.get(Constants.CONFIG_FILE_NAME)
.map(response => response.json())
.catch((error: any): any => {
return Observable.throw(error.json().error || 'Server error');
}).subscribe((configJson) => {
this.config = configJson;
});
}
public getConfig(key: string): string {
return this.config[key];
}
}
不同之处在于 this.loadJsonAndUpdateConfig() 在构造函数中的调用,而不是在 getConfig 方法中。 测试失败:
TypeError: null is not an object (evaluating 'this.config[key]')
当我进行一些调试时,订阅方法永远不会触发,就像 http.get 永远不会调用一样......
我很困惑,有人可以向我解释这种行为吗?
谢谢,
史蒂夫
【问题讨论】:
标签: angular http dependency-injection jasmine karma-jasmine