????8. ????spy
????????к????????????????????????spy??????????????
describe("A spy?? when created manually"?? function() {
var whatAmI;
beforeEach(function() {
whatAmI = jasmine.createSpy('whatAmI');  //????spy????id='whatAmI'
whatAmI("I"?? "am"?? "a"?? "spy");  //?????????????????????
});
it("is named?? which helps in error reporting"?? function() {
expect(whatAmI.and.identity()).toEqual('whatAmI');
});
it("tracks that the spy was called"?? function() {
expect(whatAmI).toHaveBeenCalled();
});
it("tracks its number of calls"?? function() {
expect(whatAmI.calls.count()).toEqual(1);
});
it("tracks all the arguments of its calls"?? function() {
expect(whatAmI).toHaveBeenCalledWith("I"?? "am"?? "a"?? "spy");
});
it("allows access to the most recent call"?? function() {
expect(whatAmI.calls.mostRecent().args[0]).toEqual("I");
});
});
????9. ??????????
????jasmine???????js????????setTimeOut??setInterval???????????tick()??????????????????????
beforeEach(function() {
timerCallback = jasmine.createSpy("timerCallback");
jasmine.clock().install();   //???
});
afterEach(function() {
jasmine.clock().uninstall();  //???
});
it("causes a timeout to be called synchronously"?? function() {
setTimeout(function() {
timerCallback();
}?? 100);
expect(timerCallback).not.toHaveBeenCalled();
jasmine.clock().tick(101);  //???????????????
expect(timerCallback).toHaveBeenCalled();
});
it("causes an interval to be called synchronously"?? function() {
setInterval(function() {
timerCallback();
}?? 100);
expect(timerCallback).not.toHaveBeenCalled();
jasmine.clock().tick(101);
expect(timerCallback.calls.count()).toEqual(1);
jasmine.clock().tick(50);
expect(timerCallback.calls.count()).toEqual(1);
jasmine.clock().tick(50);
expect(timerCallback.calls.count()).toEqual(2);
});
????10. ??????
????????done???????????????????beforeEach????done()??????????????ε???done()?????????????????????????
describe("Asynchronous specs"?? function() {
var value;
beforeEach(function(done) {   //????done????????????????
setTimeout(function() {
value = 0;
done();
}?? 1);
});
it("should support async execution of test preparation and expectations"?? function(done) {
value++;
expect(value).toBeGreaterThan(0);
done();
});
describe("long asynchronous specs"?? function() {
beforeEach(function(done) {
done();
}?? 1000);  //beforeEach??????????????????У?
it("takes a long time"?? function(done) {
setTimeout(function() {
done();
}?? 9000);
}?? 10000);  //jasmine??????????5s???????????????fail????????it?????????????????
//jasmine.DEFAULT_TIMEOUT_INTERVAL ?????????????????
afterEach(function(done) {
done();
}?? 1000);
});
describe("A spec using done.fail"?? function() {
var foo = function(x?? callBack1?? callBack2) {
if (x) {
setTimeout(callBack1?? 0);
} else {
setTimeout(callBack2?? 0);
}
};
it("should not call the second callBack"?? function(done) {
foo(true??
done??
function() {
done.fail("Second callback has been called");  //??????done.fail()???????????????fail?????
}
);
});
});
});
????11. jasmine???karma ?? grunt???
?????÷???jasmine????karma?????????????????grunt???????????????????????????????jasmine???????????grunt????????????????????karma?? anyway???????????????°ɡ?
??????????????????????????????????
????karma???
????????npm init????package.json
????????karma??????????????У?????????????
????1 npm install -g karma-cli ???????????
????2 npm install karma --save-dev
????3 npm install karma-chrome-launcher --save-dev ????????????
????4 npm install karma-jasmine --save-dev
????5 npm install jasmine-core --save-dev ?????????????????jasmine-core????????
???????karma init????karma.conf.js??????????????package.json???????????????????????
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files?? exclude)
basePath: ''??
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine']??
// list of files / patterns to load in the browser
files: [
'*.js'
]??
// list of files to exclude
exclude: [
'karma.conf.js'??
'gruntfile.js'
]??
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'src.js':'coverage'  //?????????????????
}??
// test results reporter to use
// possible values: 'dots'?? 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'?? 'coverage']??
coverageReporter:{  //??????????????????????????./coverage/index.html??
type:'html'??
dir:'coverage'??
}??
// web server port
port: 9876??
// enable / disable colors in the output (reporters and logs)
colors: true??
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_ERROR??
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true??
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Firefox']??
// Continuous Integration mode
// if true?? Karma captures browsers?? runs the tests and exits
singleRun: false??
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity??
captureTimeout: 5000??  //?????????????
})
}
????karma start karma.conf.js ??????????в?????????????????仯??
??????????????????????????·?????????·????????????.
????cmd: set FIREFOX_BIN = d:yourpathfirefox.exe
????$ export CHROME_BIN = d:yourpathchrome.exe
????????????????????????Ч
????????ο????? Karma??Jasmine????????????
????????????grunt??
????npm install grunt
????npm install grunt-karma
????npm install grunt-contrib-watch
????????gruntfile.js
module.exports = function(grunt){
grunt.initConfig({
pkg:grunt.file.readJSON('package.json')??
karma:{
unit:{
configFile:'karma.conf.js'??
//???渲??karma.conf.js??????
background: true??  //???????
singleRun: false??   //????????
}
}??
watch:{
karma:{
files:['*.js']??
tasks:['karma:unit:run']
}
}
});
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.registerTask('default'??['karma'??'watch']);
}
?????????????????grunt???????????????????????????
???????????????????????????????????????????????
????????????grunt-kamra??????
????????
???????????????????к???????????jasmine???и?????????????????????ο????????