????Karma??Jasmine??AngularJS????
???????????? ???????[ 2014/2/26 10:36:28 ] ???????????????
'use strict';
describe('MainCtrl'?? function(){
var scope;//??????????????????scope
//????????Application??鰱????????????????
beforeEach(angular.mock.module('Application'));
//???Controller????????? $rootScope ?? $controller
beforeEach(angular.mock.inject(function($rootScope?? $controller){
//?????????? scope
scope = $rootScope.$new();
//???? Controller???????????????? scope
$controller('MainCtrl'?? {$scope: scope});
});
// ??????????
});
????????????????????????????????????? scope???????????????????????????????????????????????鱾?????7?д????????????????????????????????
// ??????????
it('should have variable text = "Hello World!"'?? function(){
expect(scope.text).toBe('Hello World!);
});
|
??????????????????????????????κ????Karma???????????У?????????????
????????$resource????
???????????????????ò??? $resource ?????????????????????????? $httpBackend?? ????????汾??Angular $http?????????????????? $httpBackend ????????????? beforEach???У???? _$httpBackend_ ?????′??????????? _$httpBackend_???????????????? $httpBackend ??ζ??????????????
$httpBackend = _$httpBackend_;
$httpBackend.when('GET'?? 'Users/users.json').respond([{id: 1?? name: 'Bob'}?? {id:2?? name: 'Jane'}]);
|
?????????????
it('should fetch list of users'?? function(){
$httpBackend.flush();
expect(scope.users.length).toBe(2);
expect(scope.users[0].name).toBe('Bob');
});
|
????????????
'use strict';
describe('MainCtrl'?? function(){
var scope?? $httpBackend;//we'll use these in our tests
//mock Application to allow us to inject our own dependencies
beforeEach(angular.mock.module('Application'));
//mock the controller for the same reason and include $rootScope and $controller
beforeEach(angular.mock.inject(function($rootScope?? $controller?? _$httpBackend_){
$httpBackend = _$httpBackend_;
$httpBackend.when('GET'?? 'Users/users.json').respond([{id: 1?? name: 'Bob'}??
{id:2?? name: 'Jane'}]);
//create an empty scope
scope = $rootScope.$new();
//declare the controller and inject our empty scope
$controller('MainCtrl'?? {$scope: scope});
});
// tests start here
it('should have variable text = "Hello World!"'?? function(){
expect(scope.text).toBe('Hello World!');
});
it('should fetch list of users'?? function(){
$httpBackend.flush();
expect(scope.users.length).toBe(2);
expect(scope.users[0].name).toBe('Bob');
});
});
|
??????

???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11