首先創建環境場景:
通常三個文件夾
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 爲經過
此時這個用例經過。javascript
參考:css
https://github.com/pivotal/jasmine
html