Created
December 20, 2013 08:26
-
-
Save kechol/8051885 to your computer and use it in GitHub Desktop.
AnglarJSでテスト書くときの$httpの挙動確認。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| angular.module('runtestApp', [ | |
| 'ngResource', | |
| 'ngRoute', | |
| ]) | |
| .config(function ($routeProvider) { | |
| $routeProvider | |
| .when('/', { | |
| templateUrl: 'views/main.html', | |
| controller: 'MainCtrl' | |
| }) | |
| .otherwise({ | |
| redirectTo: '/' | |
| }); | |
| }) | |
| .run(function ($rootScope, $location, resultSrvc) { | |
| console.log('App.run() called'); | |
| $rootScope.$on('$routeChangeStart', function(event, next, current) { | |
| // call api for re-route | |
| resultSrvc.httpDeferred2().error(function() { | |
| // $location.url('/somewhere/else'); | |
| }); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| angular.module('runtestApp') | |
| .controller('MainCtrl', function ($scope) { | |
| $scope.awesomeThings = [ | |
| 'HTML5 Boilerplate', | |
| 'AngularJS', | |
| 'Karma' | |
| ]; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| describe('Controller: MainCtrl', function () { | |
| // load the controller's module | |
| beforeEach(module('runtestApp')); | |
| var MainCtrl, scope; | |
| // Initialize the controller and a mock scope | |
| beforeEach(inject(function ($controller, $rootScope) { | |
| scope = $rootScope.$new(); | |
| MainCtrl = $controller('MainCtrl', { | |
| $scope: scope | |
| }); | |
| })); | |
| it('should attach a list of awesomeThings to the scope', function () { | |
| expect(scope.awesomeThings.length).toBe(3); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| angular.module('runtestApp') | |
| .service('resultSrvc', function($http) { | |
| var self = this; | |
| this.$http = $http; | |
| return { | |
| simpleResult: function() { | |
| return true; | |
| }, | |
| httpDeferred: function() { | |
| return self.$http({ method: 'GET', url: 'http://httpstat.us/200' }); | |
| }, | |
| httpDeferred2: function() { | |
| return self.$http({ method: 'GET', url: 'http://httpstat.us/400' }); | |
| } | |
| }; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| describe('Service: resultSrvc', function () { | |
| beforeEach(module('runtestApp')); | |
| var resultSrvc, http, httpBackend, scope; | |
| beforeEach(inject(function (_resultSrvc_, $http, $httpBackend, $rootScope) { | |
| resultSrvc = _resultSrvc_; | |
| http = $http; | |
| httpBackend = $httpBackend; | |
| scope = $rootScope.$new(); | |
| })); | |
| it('should return true when simpleResult() is called', function () { | |
| expect(resultSrvc.simpleResult()).toBe(true); | |
| }); | |
| it('should define the result of httpDeferred()', function () { | |
| expect(resultSrvc.httpDeferred()).toBeDefined(); | |
| }); | |
| it('should define the result of httpDeferred() as deferred object', function ($httpBackend) { | |
| var result; | |
| httpBackend.expect('GET', 'http://httpstat.us/200').respond(200, ''); | |
| scope.$apply(function() { | |
| resultSrvc.httpDeferred() | |
| .success(function() { | |
| result = true; | |
| }) | |
| .error(function() { | |
| result = false; | |
| }); | |
| }); | |
| httpBackend.flush(); | |
| expect(result).toBe(true); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment