我以角度2编写测试用例。我想知道后续事情是如何完成的。请帮帮我。
我想要共同的代码。
common.ts
/* this used for route Mocking
* */
let mockRouter = {
navigate: jasmine.createSpy('navigate')
}
/*
* Response Url
* */
let metaUrl = {
LOGIN: '/operator/login',
META_API: '/meta-api'
};
let subject:GlobalUtils = null;
let backend:MockBackend = null;
let http:Http = null;
/*
* The beforeEach function is called once before each spec in the describe in which it is called
* */
beforeEach(()=>TestBed.configureTestingModule({
providers: [
GlobalUtils,
BaseRequestOptions,
MockBackend,
{
provide: Router, useValue: mockRouter
},
{
provide: Http,
useFactory: function (backend:ConnectionBackend, defaultOptions:BaseRequestOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
]
}));
beforeEach(inject([GlobalUtils, MockBackend, Http], (_globalUtils:GlobalUtils, mockBackend:MockBackend, _http:Http) => {
subject = _globalUtils;
backend = mockBackend;
http = _http;
}));
这背后的原因我在另一个spec.ts文件中用于写测试用例。
这是我的spec.ts文件
1.spec.ts
import {Http, BaseRequestOptions, Response, ResponseOptions, ConnectionBackend} from '@angular/http';
import {MockBackend, MockConnection} from '@angular/http/testing';
import {Router} from '@angular/router';
import {TestBed, inject} from '@angular/core/testing';
import {GlobalUtils} from '../global.utils.ts';
import { Constant } from '../constant'
describe('Global Utils : Meta urls API', () => {
/* this used for route Mocking
* */
let mockRouter = {
navigate: jasmine.createSpy('navigate')
}
/*
* Response Url
* */
let metaUrl = {
LOGIN: '/operator/login',
META_API: '/meta-api'
};
let subject:GlobalUtils = null;
let backend:MockBackend = null;
let http:Http = null;
/*
* The beforeEach function is called once before each spec in the describe in which it is called
* */
beforeEach(()=>TestBed.configureTestingModule({
providers: [
GlobalUtils,
BaseRequestOptions,
MockBackend,
{
provide: Router, useValue: mockRouter
},
{
provide: Http,
useFactory: function (backend:ConnectionBackend, defaultOptions:BaseRequestOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
]
}));
beforeEach(inject([GlobalUtils, MockBackend, Http], (_globalUtils:GlobalUtils, mockBackend:MockBackend, _http:Http) => {
subject = _globalUtils;
backend = mockBackend;
http = _http;
}));
/*
* This Test case is check Response of getMetaUrls function
* getMetaUrls must return response
* */
it('Should have get Meta urls', (done) => {
// HTTP Mocking
backend.connections.subscribe((connection:MockConnection)=> {
let options = new ResponseOptions({
body: JSON.stringify(metaUrl),status: 200
});
connection.mockRespond(new Response(options));
});
/*
* we mock http get method
* */
http
.get(Constant.metaUrl)
.subscribe((response) => {
subject.getMetaUrls();
expect(Constant.saveUrl).toEqual(metaUrl);
done();
});
});
/*
* This Test case is check empty response and redirect to error page
* getMetaUrls must return empty response
* */
it('Should have get Empty Meta urls data', (done) => {
backend.connections.subscribe((connection:MockConnection)=> {
let options = new ResponseOptions({
body: JSON.stringify(''), status: 200
});
connection.mockRespond(new Response(options));
});
http
.get(Constant.metaUrl)
.subscribe((response) => {
subject.getMetaUrls();
expect(mockRouter.navigate).toHaveBeenCalledWith(['page-error']);
done();
});
});
2.spec.ts
describe('Authentication test',()=>{
});
2.spec文件现在空白,因为我想在此文件中使用 common.ts 功能,我想知道如何。