我在测试api调用和返回错误时遇到困难
“ HttpErrorResponse:https://server.herokuapp.com/twitter/gettweets的Http错误响应:0”
我可以很好地返回正确的结果
我基本上会收到推文并在应用程序中显示
我正在测试的服务:
const TWITTER_KEY = makeStateKey('twitter');
@Injectable()
export class SocialService {
private twitter:any;
private url = 'https://server.herokuapp.com/';
constructor(private http: HttpClient,
@Inject(PLATFORM_ID) private platformId,
private transferState:TransferState) { }
initTwitter(){
this.twitter = this.transferState.get(TWITTER_KEY, null as any);
let data;
if (this.twitter) {
data = this.transferState.get(TWITTER_KEY, null);
console.log("data= "+data);
this.transferState.remove(TWITTER_KEY);
return Observable.of(data);
}else{
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
return this.http.get(this.url+'twitter/gettweets', httpOptions)
.map((response) => {
let data = response;
if (isPlatformServer(this.platformId)) {
this.transferState.set(TWITTER_KEY, data as any);
}
return data;
})
.catch((error) => Observable.throw(error) )
}
}
}
该测试的第一个测试很好,但是一旦它到达测试的错误 '如果无法获取推文,应返回错误' 我收到上述错误:
import { TestBed } from '@angular/core/testing';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import { BrowserTransferStateModule } from '@angular/platform-browser';
import { SocialService } from './social.service';
describe('SocialService', () => {
let httpMock: HttpTestingController;
let service:SocialService;
let apiURL:string
let returnedResults:any
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SocialService],
imports:[BrowserTransferStateModule, HttpClientTestingModule]
});
service = TestBed.get(SocialService);
httpMock = TestBed.get(HttpTestingController);
apiURL = 'https://server.herokuapp.com/';
returnedResults = [{created_at: "Jul 16 2018", id: 1, id_str: "1", text: "Twitter text1"}, {created_at: "Jul 17 2018", id: 2, id_str: "2", text: "Twitter text2"}]
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should get Tweets', () => {
service.initTwitter()
.subscribe((response)=>{
expect(response).toBeDefined();
expect(response).toEqual(returnedResults);
console.log(response)
})
const request = httpMock.expectOne(apiURL+'twitter/gettweets');
expect(request.request.method).toEqual('GET');
request.flush(returnedResults);
httpMock.verify();
});
it('should return error if cannot get Tweets', () => {
service.initTwitter()
.subscribe((response)=>{
expect(response).toBeDefined();
expect(response.failure.error.type).toBe('ERROR_GETTING_TWEETS');
})
const request = httpMock.expectOne(apiURL+'twitter/gettweets');
request.error(new ErrorEvent('ERROR_GETTING_TWEETS'));
httpMock.verify();
});
});
答案 0 :(得分:0)
感谢@jonrsharpe,这就是我现在正在做的事情
it('should return error if cannot get Tweets', () => {
service.initTwitter()
.subscribe(()=>{
}, error =>{
expect(error.error.type).toBe('ERROR_GETTING_TWEETS');
console.log(error.error.type)
Observable.throw(error)
}
)
const request = httpMock.expectOne(apiURL+'twitter/gettweets');
request.error(new ErrorEvent('ERROR_GETTING_TWEETS'));
httpMock.verify();
});