PhantomJS“找不到变量:require”使用PhantomJasmine时 [英] PhantomJS "Can't find variable: require" when using PhantomJasmine

查看:173
本文介绍了PhantomJS“找不到变量:require”使用PhantomJasmine时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Phantomjs和Jasmine进行单元测试javascript。 Phantomjs的网站建议我使用单独的东西来处理这个问题,特别是PhantomJasmine。我下载了PhantomJasmine并运行了示例。一切都很顺利。然后我将行 var system = require('system'); 添加到example_spec.js的顶部。当我尝试运行示例时,Phantomjs现在抛出错误我得到ReferenceError:找不到变量:require。所以,我想要做的事情就像启动浏览器一样,但看起来我无法将jasmine和phantomjs语法结合起来。我只能使用Phantomjs运行纯茉莉。我怎样才能在同一个文件中使用它们?喜欢:

  console.log('加载网页'); 
var page = new WebPage();
var url =http://www.phantomjs.org/;
page.open(url,function(status){
//页面已加载!
describe(套件,函数(){
it(包含规范)期望,function(){
expect(true).toBe(true);
});
});
phantom.exit();
});


解决方案

这不是你应该如何使用它。控制phantomjs的代码不应该与jasmine规范相混淆,反之亦然。



你应该将你的规范分成.js文件并用HTML创建标准的jasmine设置spec转换器加载规范.js文件并在phantomjs中打开该HTML文件。



另一个解决方案是使用grunt与phantomjs和jasmine自动运行jasmine规范。 / p>

https://github.com/cowboy/grunt

https://github.com/creynders/grunt-jasmine-任务


I would like to use Phantomjs and Jasmine for unit testing javascript. Phantomjs's website recommends that I use a separate thing to handle this, specifically, PhantomJasmine. I downloaded PhantomJasmine and ran the example. Everything went perfectly. I then added the line var system = require('system'); to the top of example_spec.js. The Phantomjs now throws an error when I try to run the example I get "ReferenceError: Can't find variable: require". So, I want to do need things like launch the browser, but it looks like I cannot combine jasmine AND phantomjs syntax. I can only run pure jasmin using Phantomjs. How can I use both in the same file? like:

console.log('Loading a web page');
var page = new WebPage();
var url = "http://www.phantomjs.org/";
page.open(url, function (status) {
    //Page is loaded!
    describe("A suite", function() {
      it("contains spec with an expectation", function() {
        expect(true).toBe(true);
      });
    });
    phantom.exit();
});

解决方案

It's not quite how you should be using it. The code to control phantomjs shouldn't be muddied with jasmine specs and vice versa.

You should separate your specs into .js files and create a standard jasmine setup with an HTML spec runner which loads the spec .js files and open that HTML file in phantomjs.

Another solution is to use grunt with phantomjs and jasmine to have automatically run jasmine specs.

https://github.com/cowboy/grunt
https://github.com/creynders/grunt-jasmine-task

这篇关于PhantomJS“找不到变量:require”使用PhantomJasmine时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆