我在为Cordova应用程序运行jasmine测试时遇到了一个问题:我有一个html页面(Jasmine 2.4 SpecRunner.html),其中加载了<head>
中的脚本(插件,服务,控制器等)并且<head>
中的最后一个包含文件是一个测试文件。在一个体内,我有一个deviceready
事件监听器。但是当我对Android模拟器运行测试时,我的测试总是在deviceready
之前执行,并且它看不到插件(即错误“设备未定义”)。
所以问题是:如何在deviceready
事件之后完全延迟测试文件的执行?
我的SpecRunner.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta name="msapplication-tap-highlight" content="no" />
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<title>SportsWellness</title>
<script src="scripts/winstore-jscompat.js"></script>
<link rel="shortcut icon" type="image/png" href="jasmine/lib/jasmine-2.4.1/jasmine_favicon.png">
<link rel="stylesheet" href="jasmine/lib/jasmine-2.4.1/jasmine.css">
<!-- Jasmine Modules -->
<script src="jasmine/lib/jasmine-2.4.1/jasmine.js"></script>
<script src="jasmine/lib/jasmine-2.4.1/jasmine-html.js"></script>
<script src="jasmine/lib/jasmine-2.4.1/boot.js"></script>
<!-- SportsWellness references -->
<link rel="stylesheet" href="css/app.css" />
<!-- AngularJS modules -->
<script src="libs/angular.min.js"></script>
<script src="libs/angular-resource.min.js"></script>
<script src="libs/angular-ui-router.js"></script>
<script src="libs/angular-touch.min.js"></script>
<script src="libs/angular-animate.min.js"></script>
<script src="libs/angular-mocks.js"></script>
<!-- Cordova reference, this is added to app when it's built. -->
<script src="cordova.js"></script>
<!-- Include Application scripts -->
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/router.js"></script>
<script type="text/javascript" src="js/platformOverrides.js"></script>
<!-- Include Controllers-->
<!-- Script tags -->
<!-- Include Services -->
<!-- Script tags -->
<!-- Include Directives -->
<!-- Script tags -->
<!-- Include Configuration Scripts -->
<!-- Script tags -->
<!-- Include specs to run -->
<script src="tests/ServicesTests/ReminderService.spec.js"></script>
</head>
<body>
<script type="application/javascript">
document.addEventListener('deviceready', function () {
navigator.splashscreen.hide();
}, false);
</body>
</html>
答案 0 :(得分:1)
解决方案并不像我希望的那样漂亮,但我更改了jasmine / boot.js文件,并将window.onload
函数替换为document.addEventListener('deviceready', ...)
答案 1 :(得分:0)
为什么不先运行onDeviceReady,然后在处理程序中的函数中运行脚本?
document.addEventListener("deviceready", function() {
onDeviceReady();
if ("splashscreen" in navigator) navigator.splashscreen.hide();
// other script things
}, false);
function onDeviceReady() {
// stuff
}
可以肯定它是同步的(我认为这是正确的词-它会等到inDeviceReady
函数完成后再继续)。如果这不正确,请纠正我:P