????????????????????????????????????????????????karma?????????????????????karma-junit-reporter????????????????????????????????????????????????????????
????karma??????????????????????????????????????
????????jasmine
????Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean?? obvious syntax so that you can easily write tests.
??????????jasmine???????????????????????????????????
????jasmine???????????????????????????????κ?js??????dom???????????????????API??????.
??????????????????????????????÷?
???????????????????????test.js
describe("A spec (with setup and tear-down)"?? function() {
var foo;
beforeEach(function() {
foo = 0;
foo += 1;
});
afterEach(function() {
foo = 0;
});
it("is just a function?? so it can contain any code"?? function() {
expect(foo).toEqual(1);
});
it("can have more than one expectation"?? function() {
expect(foo).toEqual(1);
expect(true).toEqual(true);
});
});
????????????????????????????????????????API????????÷??????????
?????????κ??????????????describe?????????壬??????????????????????????????????????????????????????????????????д?Щ???????????
????it?????????嵥???????????????????????????????????????????????????????????????????????????Щ???????
????expect???????????????????????????????????????????????????????????Щ?????????
????beforeEach??afterEach?????????????в?????????????????Щ???飬?????????????????????????????????????????????????????
?????????????describe?????????????????JS????????????????????????????????????it????foo????
?????????????????????????????karar?????У?????????????
????karma start test/karma.conf.js
??????????????????ng???????????????????????????.
????NG????????
???????ng????????????鶼?????di??????????????????????????????jasmine????д??????????????????angular-mock.js????????????????????鶨?壬?????????.
??????????ng-mock????Щ???÷???
????angular.mock.module ??????????window????????£???????????
????module??????????inject????????????????????????????????????????????????????????????
????beforeEach(module('myApp.filters'));
????beforeEach(module(function($provide) {
????$provide.value('version'?? 'TEST_VER');
????}));
?????????????beforeEach???????????????????????в???????????inject???????????????????
????angular.mock.inject ??????????window????????£???????????
????inject????????????????ú??ng??飬??????it???????????????????????????????
angular.module('myApplicationModule'?? [])
.value('mode'?? 'app')
.value('version'?? 'v1.0.1');
describe('MyApp'?? function() {
// You need to load modules that you want to test??
// it loads only the "ng" module by default.
beforeEach(module('myApplicationModule'));
// inject() is used to inject arguments of all given functions
it('should provide a version'?? inject(function(mode?? version) {
expect(version).toEqual('v1.0.1');
expect(mode).toEqual('app');
}));
// The inject and module method can also be used inside of the it or beforeEach
it('should override a version and test the new version is injected'?? function() {
// module() takes functions or strings (module aliases)
module(function($provide) {
$provide.value('version'?? 'overridden'); // override version here
});
inject(function(version) {
expect(version).toEqual('overridden');
});
});
});
?????????????????Щinject???????????????????inject??????????angular.inject????????????????????????????????????????????????ng?????????????????????
?????????????ng-mock???????????????????????????????????д?????????????.
????ng???????????????
????????????????????
var myApp = angular.module('myApp'??[]);
myApp.controller('MyController'?? function($scope) {
$scope.spices = [{"name":"pasilla"?? "spiciness":"mild"}??
{"name":"jalapeno"?? "spiciness":"hot hot hot!"}??
{"name":"habanero"?? "spiciness":"LAVA HOT!!"}];
$scope.spice = "hello feenan!";
});
????????д?????????
describe('myController function'?? function() {
describe('myController'?? function() {
var $scope;
beforeEach(module('myApp'));
beforeEach(inject(function($rootScope?? $controller) {
$scope = $rootScope.$new();
$controller('MyController'?? {$scope: $scope});
}));
it('should create "spices" model with 3 spices'?? function() {
expect($scope.spices.length).toBe(3);
});
it('should set the default value of spice'?? function() {
expect($scope.spice).toBe('hello feenan!');
});
});
});