0

The SUT is just:

@Injectable()
export class GetLocationService {

getPosition(): Observable<Object> {
    return Observable.create(observer => {
        navigator.geolocation.getCurrentPosition((pos: Position) => {
            observer.next(pos);
            observer.complete();
        }),
        () => {
            alert('Position is not available');
        },
        {
            enableHighAccuracy: true
        };
    });
}
}

and my test for it is the following:

export const POSITION_INFO_FAKE_JSON = {
coords: {
    latitude: 32,
    longitude: 27
}
};

describe('Get location service', () => {

beforeEachProviders(() => [
    GetLocationService
]);

it('should get current position', inject([GetLocationService],       (getLocationService) => {

    navigator.geolocation = <any>{ getCurrentPosition: function() {
        return Observable.create(observer => {
            observer.next({ json : function() { return POSITION_INFO_FAKE_JSON; }});
            observer.complete();
        });
    } };

    getLocationService.getPosition().subscribe(
        (pos: Position) => {
            expect(pos.coords.latitude).toBe(434);
            expect(pos.coords.longitude).toBe(23);
        });
}));

});

I have no idea why this test is always passing, since it definitaly should fail. Why so then? Anyone could point what I am doing wrong here ?

yankee
  • 38,872
  • 15
  • 103
  • 162
adam nowak
  • 785
  • 2
  • 9
  • 17
  • 1
    Because you are doing asynchronous work in it and are not telling jasmin that it needs to _wait_ for that work to complete. No `expect` statements are actually run before the test finishes. Read this: http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support – Sergiu Paraschiv May 19 '16 at 12:25
  • Also this: http://stackoverflow.com/questions/34607990/unit-testing-an-observable-in-angular-2 – Sergiu Paraschiv May 19 '16 at 12:28
  • ok, how would I use the done() function with the above code, since it already has a parameter? – adam nowak May 19 '16 at 12:33

0 Answers0