I am a rookie, because the recent project has used this jasmine, and in the online also did not find what Jasmine instance parsing this piece, so I would like to take this opportunity to write a few about the Jasmine instance application analysis of the blog, I hope you do not mad spray, give the newcomer a chance, in this thanked, Don't say much nonsense.
I did not have a little understanding of jasmine, even have not heard that there is such a framework exists, but the recent project in the use of this Jasmine framework, their own forced to learn a bit. First to understand what is Jasmine:
Jasmine plainly is used to test JS a framework, in fact, Jasmine's official website (http://jasmine.github.io/) also introduced a lot of examples, but I refer to his official things, few can be associated with their own projects, So go to Baidu to find the relevant information, the effect is not very good, the introduction of jasmine this information is quite small, and much the same, is a number of commonly used key functions, more detailed is (http://www.cnblogs.com/zhcncn/p/4330112. HTML), but he has some official explanations, and if you don't understand this jasmine, you might find it hard to mix and overwhelm your project. I do not have a detailed description of what the function is doing, go directly to the topic, with examples to parse, the following example I do not use the official examples, directly on the project I encountered the time I have to investigate a long period of some examples
First Instance:
The indispensable part of JS (Ajax), the request for Ajax, the usual Ajax request
Source:
$.ajax ({
URL: ' C://url ',
Type: ' GET ',
Name: ' Name ',
Data:params,
Success:function (data) {
if (data.errormsg!== ") {
Return
}
},
Error:function (XHR, Status, error) {
Console.log ("Log");
}
});
For this case of Jasmine test code to how to implement, which I have borrowed from the official website and some examples of Baidu to achieve:
Spyon ($, "Ajax"). And.callfake (Options) {
Options.success ({
errormsg = "ErrorMsg"
});
});
This spyon is the most important function in Jasmine, it is important to see in my previous two Web site introduction, using the Spyon () function to replace and monitor Ajax calls, combined with Callfake () forged AJAX return processing. Ajax's success () and error () can be used to make related forgery functions, instead of Ajax functions, to return the required data to the measured function. Here are the error scenarios:
Spyon ($, "Ajax"). And.callfake (Options) {
var XHR = {
"Status": "NG"
};
var Status = "Error";
var error= {
"Message": "It has an errormessage"
};
Options.error (XHR, Status, error);
});
Remember this function,callfake (), replace the function being monitored, the original function does not execute, the function inside is executed
The first part first introduced here, the second part of the time to update, because they are also a rookie, may be in the description of the time is not clear, I hope you have more comments, I will try to correct, there is not clear can leave a message, I will answer
Jasmine Example 01