There are a couple of issues in your UserService.
You're using
Q
, rather than$q
. Hard to know exactly what effect this has, other than it's not typical when using Angular and might have affects with regards to exactly whenthen
callbacks run.You're actually creating a promise in
getUser
when you don't really need to (can be seen as an anti-pattern). Thesuccess
function of the promise returned from$http
promise I think is often more trouble than it's worth. In my experience, usually better to just use the standardthen
function, as then you can return a post-processed value for it and use standard promise chaining:function getUser(user) { if (user === undefined) { user = getUserId(); } return $http({ method: 'GET', url: BASE_URL + '/users/' + user }).then(function(response) { return response.data; }); }
Once the above is changed, the controller code can be changed to
UserService.getUser($routeParams.contactId).then(function (data) {
$scope.contacts = data;
});
Then in the test, after resolving the promise call $apply
.
def.resolve(contact);
scope.$apply();