Skip to content Skip to sidebar Skip to footer

Angular 4 Unit Test Get Error - Failed: Cannot Read Property 'map' Of Undefined

I'm trying to get success on Angular 4 unit test, but I'm getting differents error, I'm getting the error Failed: Cannot read property 'map' of undefined. I'm using .map() only on

Solution 1:

Currently you are mocking the HubWrapperComponent correctly, such that it will not make a call to your back end every time you invoke get. In order to get this working, you'll need to use Jasmine's Spy utility, returnValue to tell the test what should be returned when http.get is called. Right now, this is not specified so the map is being called on undefined.

In your spec file, after the line:

mockHub = jasmine.createSpyObj('hubWrapperComponent', ['get']);

Add this line:

mockHub.get.and.returnValue(// your test response);

Based on the code in the service, my assumption is that you are expecting an Observable from the get method, so your returnValue call will look something like this:

mockHub.get.and.returnValue(Observable.of(
    { 
      json: () => { 
        return // object with properties you want to test 
      } 
    }
  )
);

More information on this method here: https://jasmine.github.io/api/2.7/Spy_and.html.


Post a Comment for "Angular 4 Unit Test Get Error - Failed: Cannot Read Property 'map' Of Undefined"