jeudi 17 novembre 2016

How to simulate the failure of mock service in karma?

Obeservable function in example.component.ts

public name: string;

this._cm.getData().subscribe(
    response => {
       this.name = response.name;
    },
    error => {
        this.name = undefined;
    }

Mock service

public _cm = {
    data: {
        name: 'test'
    }

    getData(): Observable<any> {
        return Observable.of(this.data);
    }
}

Test observable function in example.component.spec.ts

// this test work corectly
it('Should set name property to "test"', () => {
    comp._cm.getData();
    expect(comp.name).toBe('test');  
}

it('Should set name property to undefined', () => {
    comp._cm.getData();
    expect(comp.name).toBe('undefined');  
}

but I do not know how to simulate error response getData()

Aucun commentaire:

Enregistrer un commentaire