I am trying to set up a http GET request expectation via jasmine but I'm getting following error:
TypeError: Cannot read property 'user' of undefined
The service function I want to test looks like:
function getData() {
return $http.get('http://example.com/data')
.then(function (response) {
return response.data.example.user;
});
}
A GET request to the URL "http://example.com/data" returns following data:
{
"id": "1",
"example": {
"user": [
{
"name": "John",
"wife": [
{
"name": "Jane",
"age": "27"
}
]
}
}
}
The corresponding test looks like this:
describe("Service: myService", function () {
beforeEach(module("myApp"));
var myService, $httpBackend;
beforeEach(inject(function (_myService_, _$httpBackend_) {
myService = _myService_;
$httpBackend = _$httpBackend_;
$httpBackend.whenGET("http://example.com/data").respond("some data");
}));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("should call the correct URL", function() {
$httpBackend.expectGET("http://example.com/data");
myService.getData();
$httpBackend.flush();
});
Somehow I am not able to test a http GET request as soon as the service function (that I want to test) returns a nested JSON property instead of the entire JSON.
Thanks in advance!