I follow the official website of the process to write an isolated service test, but there is a problem, I guess the data is not recognized, I am a novice, I do not know what this is caused by this.
test service get should have expected fake strategys (then) FAILED
//strategyTables.service.ts
@Injectable()
@BaseUrl("http://localhost:8080/api/")
@DefaultHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json'
})
export class StrategyTablesService extends RESTClient {
public constructor(http: Http) {
super(http);
}
protected requestInterceptor(req: Request) {
}
protected responseInterceptor(res: Observable<Response>): Observable<Response> {
return res;
}
@GET("strategys/")
public getStrategys( @Query("sort") sort?: string ): Observable<any> { return null; };
// 分页条件查询
@GET("controller/getstrategys/strategyname={strategyname}&isuseragent={isuseragent}&iscookie={iscookie}&&size={size}&page={page}&sorts={sorts}")
public getAllstrategys( @Path('strategyname') strategyname:string ,@Path('isuseragent') isuseragent:string ,@Path('iscookie') iscookie:string ,@Path('page') page:number,
@Path('size') size:number, @Path('sorts') sorts:string): Observable<any> { return null; };
@GET("strategys/{id}")
public getStrategyById( @Path("id") id: string ): Observable<any> { return null; };
@PUT("strategys/{id}")
public updateStrategyById( @Path("id") id: number, @Body strategy: Strategy): Observable<any> { return null; };
@DELETE("strategys/{id}")
public deleteStrategyById( @Path("id") id: number): Observable<any> { return null; };
@POST("controller/savestrategy")
public createStrategy(@Body strategy: Strategy): Observable<any> { return null; };
}
//strategyTables.service.spec.ts
import {StrategyTablesService} from "./strategyTables.service";
import {Http, HttpModule, XHRBackend,Response, ResponseOptions} from "@angular/http";
import {TestBed, inject, async} from "@angular/core/testing";
import {MockBackend,MockConnection} from"@angular/http/testing";
import {Strategy} from "../domain/strategy";
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/toPromise';
const makeStrategyData = () => [
{
"id": 20,
"strategyname": "abcee",
"isuseragent": "?",
"depth": 10,
"download_delay": 3,
"iscookie": "?",
"agent": "2",
"starttime": "2017?4?3? 20:7:28"
},
{
"id": 19,
"strategyname": "efg",
"isuseragent": "?",
"depth": 10,
"downloadDelay": 3,
"iscookie": "?",
"agent": "2",
"starttime": "2017?4?3? 20:5:39"
},
{
"id": 18,
"strategyname": "abcde",
"isuseragent": "?",
"depth": 10,
"download_delay": 3,
"iscookie": "?",
"agent": "2",
"starttime": "2017?4?3? 20:0:17"
},
{
"id": 16,
"strategyname": "11",
"isuseragent": "?",
"depth": 2,
"download_delay": 3,
"iscookie": "?",
"agent": "2",
"starttime": "2017?4?3? 19:48:21"
},
{
"id": 14,
"strategyname": "abc",
"isuseragent": "?",
"depth": 2,
"download_delay": 3,
"iscookie": "?",
"agent": "2",
"starttime": "2017?4?3? 19:48:4"
}
] as Strategy[];
describe('test service',()=> {
beforeEach(()=>{
TestBed.configureTestingModule({
imports:[HttpModule],
providers:[
StrategyTablesService,
{ provide: XHRBackend, useClass: MockBackend }
]
});
});
it('can instantiate service when inject service ',inject([StrategyTablesService],(service:StrategyTablesService) =>{
expect(service instanceof StrategyTablesService).toBe(true);
}));
it('can instantiate service with "new"', inject([Http], (http: Http) => {
expect(http).not.toBeNull('http should be provided');
let service = new StrategyTablesService(http);
expect(service instanceof StrategyTablesService).toBe(true, 'new service should be ok');
}));
it('can provide the mockBackend as XHRBackend',
inject([XHRBackend], (backend: MockBackend) => {
expect(backend).not.toBeNull('backend should be provided');
}));
describe('get',()=> {
let backend: MockBackend;
let service: StrategyTablesService;
let fakeStrategy: Strategy[];
let response: Response;
beforeEach(inject([Http, XHRBackend], (http: Http, be: MockBackend) => {
backend = be;
service = new StrategyTablesService(http);
fakeStrategy = makeStrategyData();
let options = new ResponseOptions({status: 200, body: {data: fakeStrategy}});
response = new Response(options);
}));
it('should have expected fake strategys (then)', async(inject([], () => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));
service.getAllstrategys("","","",0,5,"id").toPromise()
// .then(() => Promise.reject('deliberate'))
.then(strategys => {
expect(strategys.length).toBe(fakeStrategy.length,
'should have expected no. of strategys');
});
})));
});
});
Expected undefined to be 5, 'should have expected no. of strategys'.
Aucun commentaire:
Enregistrer un commentaire