xxxxxxxxxx
<html ng-app="demoApp">
<head>
<meta charset="utf-8">
<meta name="description" content="Jasmine - Unit Test Promises">
<title>Jasmine - Unit Test Promises</title>
<!-- jasmine -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.js"></script>
<!-- jasmine's html reporting code and css -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine-html.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.css" rel="stylesheet" />
<!-- angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.js"></script>
<!-- angular's testing helpers -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular-mocks.js"></script>
</head>
<body>
<h1>Jasmine - Unit Test Promises</h1>
<p>By <a href="https://brianmcd.com/2014/03/27/a-tip-for-angular-unit-tests-with-promises.html">Brianmcd</a></p>
<p>The example does not run without <code>$rootScope.$digest();</code>. Actually the first test does not complain. Which is one reason to do unit tests.</p>
<script id="jsbin-javascript">
var app = angular.module('demoApp', []);
describe('how to test with promises', function () {
var deferred;
beforeEach(function () {
inject(function ($q, _$rootScope_) {
$rootScope = _$rootScope_;
deferred = $q.defer();
});
});
it('One way to test', function () {
deferred.promise.then(function (value) {
expect(value).toBe(10);
});
deferred.resolve(10);
$rootScope.$digest();
});
it('Another way to test', function () {
var handler = jasmine.createSpy('success');
deferred.promise.then(handler);
deferred.resolve(10);
$rootScope.$digest();
expect(handler).toHaveBeenCalledWith(10);
});
});
var jasmineEnv = jasmine.getEnv();
// Add detailed HTML-formatted results
jasmineEnv.addReporter(new jasmine.HtmlReporter());
// Execute the tests!
jasmineEnv.execute();
</script>
<script id="jsbin-source-html" type="text/html">
<html ng-app="demoApp">
<head>
<meta charset="utf-8">
<meta name="description" content="Jasmine - Unit Test Promises">
<title>Jasmine - Unit Test Promises</title>
<\!-- 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 -->
<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>
</head>
<body>
<h1>Jasmine - Unit Test Promises</h1>
<p>By <a href="https://brianmcd.com/2014/03/27/a-tip-for-angular-unit-tests-with-promises.html">Brianmcd</a></p>
<p>The example does not run without <code>$rootScope.$digest();</code>. Actually the first test does not complain. Which is one reason to do unit tests.</p>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">var app = angular.module('demoApp', []);
describe('how to test with promises', function () {
var deferred;
beforeEach(function () {
inject(function ($q, _$rootScope_) {
$rootScope = _$rootScope_;
deferred = $q.defer();
});
});
it('One way to test', function () {
deferred.promise.then(function (value) {
expect(value).toBe(10);
});
deferred.resolve(10);
$rootScope.$digest();
});
it('Another way to test', function () {
var handler = jasmine.createSpy('success');
deferred.promise.then(handler);
deferred.resolve(10);
$rootScope.$digest();
expect(handler).toHaveBeenCalledWith(10);
});
});
var jasmineEnv = jasmine.getEnv();
// Add detailed HTML-formatted results
jasmineEnv.addReporter(new jasmine.HtmlReporter());
// Execute the tests!
jasmineEnv.execute();</script></body>
</html>
Modified http://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.js to a secure url
Modified http://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine-html.js to a secure url
Modified http://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.js to a secure url
Modified http://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular-mocks.js to a secure url
https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.js
https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine-html.js
https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.js
https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular-mocks.js
https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.js
https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine-html.js
https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.js
https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular-mocks.js