Skip to content

Instantly share code, notes, and snippets.

@benlesh
Last active August 29, 2015 13:56
Show Gist options
  • Save benlesh/8865973 to your computer and use it in GitHub Desktop.
Save benlesh/8865973 to your computer and use it in GitHub Desktop.
Angular - Testing Promises and Nested Promises
var app = angular.module('myApp', []);
<!DOCTYPE html>
<html>
<head>
<!-- jasmine -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.js"></script>
<!-- jasmine's html reporting code and css -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine-html.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.css" rel="stylesheet" />
<!-- angular itself -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.js"></script>
<!-- angular's testing helpers -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular-mocks.js"></script>
<!-- your angular app code -->
<script src="app.js"></script>
<script src="people.js"></script>
<script src="MainCtrl-better.js"></script>
<!-- your Jasmine specs (tests) -->
<script src="specs.js"></script>
</head>
<body>
<!-- bootstrap jasmine! -->
<script>
var jasmineEnv = jasmine.getEnv();
// Tell it to add an Html Reporter
// this will add detailed HTML-formatted results
// for each spec ran.
jasmineEnv.addReporter(new jasmine.HtmlReporter());
// Execute the tests!
jasmineEnv.execute();
</script>
</body>
</html>
angular.module('myApp').controller('MainCtrl', ['$scope', 'people', function($scope, people) {
var self = this;
self.personSuccess = function(result) {
};
self.personFail = function(result) {
$scope.errorMessage = 'failed to load person';
};
self.specialDataSuccess = function(person) {
person.
}
$scope.personId = 123;
$scope.loadPerson = function (){
people.getPerson($scope.personId).then(function(result) {
var person = result.data;
if(person.specialDataKey) {
people.getSpecialData(person.specialDataKey).then(function (result) {
person.specialData = result.data;
});
}
$scope.person = person;
};
};
});
angular.module('myApp').controller('MainCtrl', ['$scope', 'people', function($scope, people) {
$scope.personId = 123;
$scope.loadPerson = function (){
people.getPerson($scope.personId).then(function(result) {
var person = result.data;
if(person.specialDataKey) {
people.getSpecialData(person.id, person.specialDataKey).then(function (result) {
person.specialData = result.data;
}, function() {
$scope.errorMessage = 'failed to load special data.';
});
}
$scope.person = person;
}, function () {
$scope.errorMessage = 'failed to load person';
});
};
});
angular.module('myApp').factory('people', ['$http', function($http) {
return {
getPerson: function(id) {
return $http.get('/person/' + id);
},
getSpecialData: function (id, key){
return $http.get('/person/' + id + '/specialData/' + key);
}
};
});
@Apollo101
Copy link

I'm confused, where are the tests? I'm having troubles testing nested promises in my own app and I can't seem to find any decent examples anywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment