????????karma runner
????????????????jasmine????????????jasmine-jQuery??jasmine-ajax??????????karma???runner??????????????????????????karma?????????
??????????????????? 1??karma??????jasmine-jQuery??jasmine-ajax(????????bowerinstall) 2??host ?????html fixtures?? 3??????html fixtures ??install ajax????
????????host ???karma????pattern????????karma???????
files: [
{
pattern: 'view/**/*.html'??
watched: true??
included: false??
served: true
}??
'bower_components/jquery/dist/jquery.js'??
'bower_components/jasmine-jquery/lib/jasmine-jquery.js'??
'bower_components/jasmine-ajax/lib/mock-ajax.js'??
'src/*.js'??
'test/*.js'
]??
??????????????karma?????jasmine???汾??????????????npm install karma-jasmine@2.0????μ?karma-jasmine?????
????????????????karma?????????????????????????????demo on github.
????????html fixtures?????
describe('console html content'?? function() {
beforeEach(function() {
jasmine.getFixtures().fixturesPath = 'base/view/';
loadFixtures("index.html");
});
it('index html'?? function() {
expect($("h2")).toBeInDOM();
expect($("h2")).toContainText("this is index page");
});
})
    ????mock ajax??
describe('console html content'?? function() {
beforeEach(function() {
jasmine.Ajax.requests.when = function(url) {
return this.filter("/jquery/ajax")[0];
};
jasmine.Ajax.install();
});
it('index html'?? function() {
expect($("h2")).toBeInDOM();
expect($("h2")).toContainText("this is index page");
});
it("ajax mock"?? function() {
var doneFn = jasmine.createSpy("success");
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(args) {
if (this.readyState == this.DONE) {
doneFn(this.responseText);
}
};
xhr.open("GET"?? "/some/cool/url");
xhr.send();
expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url');
expect(doneFn).not.toHaveBeenCalled();
jasmine.Ajax.requests.mostRecent().response({
"status": 200??
"contentType": 'text/plain'??
"responseText": 'awesome response'
});
expect(doneFn).toHaveBeenCalledWith('awesome response');
});
it("jquery ajax success with getResponse"?? function() {
var result;
getResponse().then(function(data){
result = data;
});
jasmine.Ajax.requests.when("/jquery/ajax").response({
"status": 200??
"contentType": 'text/plain'??
"responseText": 'data from mock ajax'
});
expect(result).toEqual('data from mock ajax');
});
it("jquery ajax error"?? function() {
var status;
$.get("/jquery/ajax").error(function(response) {
status = response.status;
});
jasmine.Ajax.requests.when("/jquery/ajax").response({
"status": 400
});
expect(status).toEqual(400);
});
afterEach(function() {
jasmine.Ajax.uninstall();
});
})