????1. ????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.This guide is running against Jasmine version 2.4.1.
????????????Jasmine??????ж?????????????JS????????????
????Jasmine github???
???????????: ??????????????demo??????????
????Jasmine????: ???????????????????SpecRunner.html?? ?????jasmine???????????
????2. ???????
??????Qunit????????????????????????????
?????????????????????????????grunt????????????????
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine v2.4.1</title>
<!-- jasmine???????????? -->
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.4.1/jasmine_favicon.png">
<link rel="stylesheet" href="lib/jasmine-2.4.1/jasmine.css">
<script src="lib/jasmine-2.4.1/jasmine.js"></script>
<script src="lib/jasmine-2.4.1/jasmine-html.js"></script>
<script src="lib/jasmine-2.4.1/boot.js"></script>
<!-- ????????? -->
<script src="src/SourceForTest.js"></script>
<!-- ???????? -->
<script src="spec/TestCases.js"></script>
</head>
<body>
</body>
</html>
????????????????
????describe("A suite"?? function() {
????it("contains spec with an expectation"?? function() {
????expect(true).toBe(true);
????expect(false).not.toBe(true);
????//fail("test will fail if this code excute");  //???????????е??????
????});
????});
????describe?????????????????
????it????????????????
?????????e?BDD??????дdescribe??it????????
????????BDD??ο??????BDD???????????
????expect()????????????????????toBe()?????????????===??not????????
????jasmine???и??????????????????????????????????????
????3. SetUp??TearDown
??????jasmine????beforeEach??afterEach??beforeAll??afterAll?????
describe("A spec using beforeEach and afterEach"?? function() {
var foo = 0;
beforeEach(function() {
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);
});
});
????4. ????????(matcher)
????????custom_matcher???
beforeEach(function(){
jasmine.addMatchers({
toBeSomeThing: function(){  //????????????
return {
compare: function (actual?? expected) {  //compare??????
var foo = actual;
return {
pass: foo === expected || 'something' ??
message: "error message here"  //?????false??????
}  //??????????pass????????pass??????????????
//negativeCompare: function(actual?? expected){ ... }  //?????not.???÷?
}
};
}
});
});
????5. this???÷?
????????????it???????this?????????????????????????????????????????????????????????
describe("A spec"?? function() {
beforeEach(function() {
this.foo = 0;
});
it("can use the `this` to share state"?? function() {
expect(this.foo).toEqual(0);
this.bar = "test pollution?";
});
it("prevents test pollution by having an empty `this` created for the next spec"?? function() {
expect(this.foo).toEqual(0);
expect(this.bar).toBe(undefined);  //true
});
});
????6. ???ò???
??????describe??it??????x?????xdescribe??xit??????????ò????????????????????????????????????
????7. Spy
???????????????????????????????????????????????ε???????????α????????????ο????????
describe("A spy"?? function() {
var foo?? bar = null;
beforeEach(function() {
foo = { setBar:function(value){bar = value;} };
spyOn(foo?? 'setBar');   //??????趨??????????????
foo.setBar(123);
foo.setBar(456?? 'another param');
});
it("tracks that the spy was called"?? function() {
expect(foo.setBar).toHaveBeenCalled();
});
it("tracks that the spy was called x times"?? function() {
expect(foo.setBar).toHaveBeenCalledTimes(2);
});
it("tracks all the arguments of its calls"?? function() {
expect(foo.setBar).toHaveBeenCalledWith(123);
expect(foo.setBar).toHaveBeenCalledWith(456?? 'another param');
});
it("stops all execution on a function"?? function() {
expect(bar).toBeNull();
});
});