引入jasmine測試架構
1、下載下傳jasmine測試架構
http://jasmine.github.io
選擇RELEASES版本
2、Spring Boot項目引入jasmine測試架構
把lib/jasmine-2.8.0引入到static目錄下
把lib/jasmine-2.8.0引入到static目錄下
3、index.html引入jasmine
index.html引入jasmine
4、編寫jasmine測試代碼
index.html代碼:
<body>
<a href="#" target="_blank" rel="external nofollow" οnclick="get1()">發生get請求</a>
<script type="text/javascript">
function get1(){
$.getJSON("http://localhost:8081/test/get1").then(
function(result){
console.log(result)
}
)
}
// 每一個測試用例的逾時時間
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
// 請求的接口的字首 // http://localhost:8080/test
var base = "http://localhost:8081/test";
//測試子產品
describe("曉風輕-ajax跨越完全講解", function() {
// 測試方法
it("get1請求", function(done) {
// 伺服器傳回的結果
var result;
$.getJSON(base + "/get1").then(function(jsonObj) {
result = jsonObj;
});
// 由于是異步請求,需要使用setTimeout來校驗
setTimeout(function() {
expect(result).toEqual({
"data" : "get1 ok"
});
// 校驗完成,通知jasmine架構
done();
}, 100);
});
});
</script>
<h2>test</h2>
</body>
5、驗證jasmine測試代碼
兩個問題:
(1)跨域錯誤
(2)跨域錯誤導緻結果傳回為undifined,是以結果跟Expected值不相等,即undifined != “get1 ok”