【问题标题】:Unit testing ext js with Jasmine 2.0 and PhantomJS使用 Jasmine 2.0 和 PhantomJS 对 ext js 进行单元测试
【发布时间】:2014-04-23 15:46:14
【问题描述】:

我正在尝试为我的 ext js 应用程序设置单元测试。
我正在使用 Jasmine 2.0 和 PhantomJS 从控制台运行测试。
我可以在控制器的init方法中成功初始化store。
但是,如果我尝试在商店配置中声明它,我会收到以下错误:
TypeError: 'null' 不是构造函数(评估 'new c()')(第 1 行)(1) ,
错误的原因是什么,如何解决?

提前谢谢你。

我的代码如下:

TestApplication.js

Ext.Loader.setConfig({ enabled: true });
Ext.ns('myApp');

// Loading different components like controller, model, view..
Ext.application({
    name: 'myApp',
    appFolder: '../App',
    controllers: [],
    autoCreateViewport: false,

    init : function() {
        myApp.app = this;
    },

    // Launch Jasmine test environment
    launch: function () {
        var jasmineEnv = jasmine.getEnv();
        jasmineEnv.updateInterval = 1000;
        var htmlReporter = new jasmine.HtmlReporter();
        jasmineEnv.addReporter(htmlReporter);
        jasmineEnv.execute();
    }

});

spec.js

describe("myController", function () {
    var ctrl= null,
        store = null;

    beforeEach(function () {
        bmTab = Ext.create("myApp.controller.myController");
        bmTab.init();
    });
});

myController.js

Ext.define('myApp.controller.myController', {
    extend: 'Ext.app.Controller',

    //stores: [Stores.myStore];

    init:function() {
        console.log('**** init');
        var store = Ext.create(Stores.myStore);

        console.log('**** store created' + store);
    }    
});

【问题讨论】:

  • 我不确定Stores.myStore是否会在指定存储初始化时正确解析,您可以通过硬编码指定Stores.myStore的值来尝试一下stores 配置?
  • @Cyclone 你的意思是明确指定完整的命名空间吗?我试过了。结果相同
  • 嗯 :-/ 我想知道还有什么问题...
  • 我希望//stores: [Stores.myStore]; 语句末尾的分号是错字,对吗? :) 因为它应该是一个逗号。
  • @Cyclone,也尝试过使用别名。错误仍然显示。

标签: javascript unit-testing extjs jasmine phantomjs


【解决方案1】:

问题在于使用 Jasmine 2.0,而所有教程都使用 Jasmine 1.3
在 Jasmine 2.0 中引入了一个文件 boot.js
它在window.onload 上调用jasmine.getEnv().execute()
因此,规范在 Ext.launch 被调用之前执行。 一旦我从 boot.js 中删除了对 execute() 的调用,它就开始工作了。 下面是我的 TestApplication.js 代码的最终版本

附: 注意,HtmlReporter也是在boot.js中初始化的,所以不需要在Ext.launch函数中初始化

Ext.Loader.setConfig({ enabled: true });

Ext.application({
    name: 'myApp',
    appFolder: '../App',
    controllers: [],
    autoCreateViewport: false,

    // Launch Jasmine test environment
    launch: function () {
        var jasmineEnv = jasmine.getEnv();
        jasmineEnv.updateInterval = 1000;
        jasmineEnv.execute();
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-03
    • 2016-08-14
    • 2015-10-26
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    相关资源
    最近更新 更多