预期未定义为等于2

时间:2017-07-29 13:03:25

标签: javascript angular ionic2 karma-jasmine angular2-testing

使用的技术: Karma / Jasmine,Angular2

service.ts

Injectable()
export class Service { 

    constructor(private http: Http) { }
      getGoogle(): Observable<any>{
        console.log("Inside service");
        return this.http.get('https://jsonplaceholder.typicode.com/posts/1');    
     }}

请忽略错别字,所有导入都正确。在UI中正确调用了。

service.spec.ts

describe('Provider:Service', () => {

const HERO_ONE = 'HeroNrOne';
const HERO_TWO = 'WillBeAlwaysTheSecond';

    let lastConnection;
    let service;


    beforeEach(() => {
        let injector = ReflectiveInjector.resolveAndCreate([
            { provide: ConnectionBackend, useClass: MockBackend },
            { provide: RequestOptions, useClass: BaseRequestOptions },
            Http,
            Service

        ]);
        service = injector.get(Service);
        let backend = injector.get(ConnectionBackend) as MockBackend;
        backend.connections.subscribe((connection: any) => lastConnection = connection);

    }); 

  it('getGoogle() should return the data in json format', fakeAsync(() => {
        console.log('1');
        let result:String[];
        service.getGoogle().toPromise().then((heroes:String[]) => result = heroes);
        lastConnection.mockRespond(new Response(new ResponseOptions({
            body:JSON.stringify({data: [HERO_ONE, HERO_TWO]}),
          })));
        tick();
        console.log(result[0]);
        console.log('3');

        expect(result.length).toEqual(2, 'should contain given amount of 
        heroes');   //this spec is failing because results does is not getting the response.  

    }));

result:String[]未获得提供的回复。

0 个答案:

没有答案