如何通过 phantomjs 可靠地执行利用 requirejs 的 Jasmine 测试
How do I reliably execute Jasmine tests that utilize requirejs via phantomjs?
我正在使用phantomjs来运行茉莉花测试。我的茉莉花测试在描述块周围使用 require,以确保加载所有正确的模块。
我的测试不会运行,因为page.evaluate -> jasmine.getEnv().execute();
在requirejs完成加载模块之前运行。
我想知道是否有人知道解决这个问题的真正好方法。我有一个答案,我将在下面发布,但很想通过其他答案比较笔记。如果你的更好,我会选择它作为答案显然:)
我做了一些不同的事情 - 我的HTML页面有一个函数,包装在require((调用中:
var run_tests = function (specfiles) {
require(specfiles, function () {
jasmine.getEnv().execute();
});
};
然后我page.evaluate( function (test_specs) { run_tests(test_specs) }, ['test1.spec', 'test2.spec']);
我的解决方案,jQuery可用,是这样的:
在运行任何测试之前加载配置文件。
var jasmine_deferreds = [];
// Setup an event to fire on the document
// I actually did this with native code rather than jquery because
// I wanted to minimize jquery usage
// ....
// setTimeout so all files loaded after this will finish registering their requires
setTimeout( function() {
$.when.apply( null, jasmine_deferreds ).then( function() {
// Fire event that was created
});
}, 5 );
这取决于您希望如何构建延迟数组,然后解决它们。我基本上推送到数组,然后在完成需求时解决。我用我自己的版本包装了 require,该版本知道在完成后自动解决它 - 所以我不需要在每次测试中手动推送和解决。
然后在我的幻影文件中,我这样做:
page.evaluate ->
mylistener = ( document ) -> jasmine.getEnv().execute();
document.addEventListener( 'test_ready_event', mylistener, false);
这使得我知道我所有需要的模块都已加载,而不会有一些任意setTimeout
一旦我加载了太多文件,这些可能会太短。我正在使用的那个setTimeout
是安全的,因为它只用于在主调用堆栈完成后触发。它并不真正关心时间。
相关文章:
- 使用jasmine模拟对服务器的调用
- 以可优化的方式使用requirejs加载模板
- RequireJS向模块传递配置总是返回undefined
- 为什么jasmine期望不验证是否抛出了错误
- 模拟chrome.storage.local函数使用Jasmine
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- 为什么spyOn"停止函数“”的所有执行;Jasmine(要求澄清Jasmine 2.2间谍文件)
- PhoneGap/带有RequireJS's的文本插件
- 如何通过 phantomjs 可靠地执行利用 requirejs 的 Jasmine 测试
- Jasmine 和 requireJs - 不匹配的匿名 define() 模块:
- Jasmine + JSTestDriver + Coverage + RequireJS
- 使用 requireJs 模拟 Jasmine 中的匿名模块调用
- PhantomJS不能很好地与RequireJS+Jasmine配合使用
- Webpack karma jasmine-requirejs在尝试运行测试时出错
- 如何使requirejs与jasmine和blanketjs一起工作以覆盖代码
- 让AngulaJS + Angular AMD + RequireJS与Karma和Jasmine一起工作时出错
- 使用Jasmine测试RequireJS
- 让 requirejs 与 Jasmine 一起工作
- “错误:不匹配的匿名 define()” 与 Karma + RequireJS + Jasmine
- 监视由jQuery调用的函数“更改”与RequireJS和Jasmine