单元测试时提供假CKEDITOR

时间:2017-11-24 14:36:31

标签: javascript unit-testing

我有2个项目..

.
├── dependency-project
│   └── ckeditor
│       └── ckeditor.js
└── my-project
    ├── app
    │   └── script.js
    ├── karma.conf.js
    └── test
        └── script.test.js

dependency-project包含ckeditor.js,两个应用程序都在同一个Web服务器上提供。

因此my-project可以根据我配置网络服务器的方式,通过ckeditor.js访问http://localhost:8080/ckeditor/ckeditor.js

通过浏览器使用应用程序时此设置正常。但是对于单元测试,当我的测试尝试使用CKEDITOR时,我收到错误。

注意:我没有在ckeditor.js中引用karma.conf.js,如果可能的话,我想定义一个假的。

这是错误,有没有办法在这里存根/模拟CKEDITOR? ..

      PhantomJS 2.1.1 (Mac OS X 0.0.0)
        ReferenceError: Can't find variable: CKEDITOR

script.js ..

document.getElementById('myElement').addEventListener('click', _editingTextStart);

function _editingTextStart() {
  CKEDITOR.disableAutoInline = true;
}

script.test.js ..

'use strict';
describe('script', function() {

  var sandbox;
  beforeEach(function() {
    sandbox = sinon.sandbox.create();
  });
  afterEach(function() {
    sandbox.restore();
  });

  it('should setup ckeditor', function() {
    var event = new Event('click');
    document.getElementById('myElement').dispatchEvent(event);
  });
});

1 个答案:

答案 0 :(得分:0)

决定使用以下方法模拟对象本身:

window['CKEDITOR'] = { disableAutoInline: false };

在测试设置中。

相关问题