引入jasmine测试框架
引入jasmine测试框架
1、下载jasmine测试框架


2、Spring Boot项目引入jasmine测试框架


3、index.html引入jasmine

4、编写jasmine测试代码
index.html代码:
<body>
<a href="#" onclick="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”