首先建立环境场景:
一般三个文件夹
lib jasmine的系统文件存放文件夹
spec 写測试用例的文件夹
src 存放源码的文件夹(被測对象)
specRunner.html 測试入口文件。
入口文件内容:
--------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-core/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="lib/jasmine-core/jasmine.css">
<script type="text/javascript" src="lib/jasmine-core/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-core/jasmine-html.js"></script>
<script type="text/javascript" src="lib/jasmine-core/boot.js"></script>
<!-- include source files here... -->
<script type="text/javascript" src="js_file_要測试的源码.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="spec/Spec測试用例文件.js"></script>
<script type="text/javascript">
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
</script>
</head>
<body>
</body>
</html>
--------------------------
在 spec 文件夹中,写个測试用例。写例如以下内容:
-------------------------
describe("This is an exmaple suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
expect(false).toBe(false);
expect(false).not.toBe(true);
});
});
-------------------------
測试三个用例演示样例。
1,true == true 为通过
2。false == false 为通过
3,false != true 为通过
此时这个用例通过。
參考: