背景如何定義的順序
我工作中的Node.js程序,寫在我的摩卡測試套件用柴和SinonJS運行摩卡測試。我有核心圖形模塊,可以控制對上下文的訪問。
由於node-webgl
的工作原理,我只希望爲整個測試運行初始化一次上下文。我有一些測試希望在覈心模塊初始化之前運行,如下所示:
describe('module:core', function() {
describe('pre-init', function() {
describe('.isInitialized', function() {
it('should return false if the module is not initialized', function() {
expect(core.isInitialized()).to.be.false;
});
});
describe('.getContext', function() {
it('should error if no context is available', function() {
expect(function() {
core.getContext();
}).to.throw(/no context/i);
});
});
});
describe('.init', function() {
it('should error on an invalid canvas', function() {
expect(function() {
core.init(null);
}).to.throw(/undefined or not an object/i);
expect(function() {
core.init({});
}).to.throw(/missing getcontext/i);
});
it('should error if the native context could not be created', function() {
var stub = sinon.stub(global._canvas, 'getContext').returns(null);
expect(function() {
core.init(global._canvas);
}).to.throw(/returned null/i);
stub.restore();
});
it('should initialize the core module', function() {
expect(function() {
core.init(global._canvas);
}).not.to.throw();
});
});
describe('post-init', function() {
describe('.isInitialized', function() {
it('should return true if the module is initialized', function() {
expect(core.isInitialized()).to.be.true;
});
});
describe('.getContext', function() {
it('should return the current WebGL context', function() {
var gl = null;
expect(function() {
gl = core.getContext();
}).not.to.throw();
// TODO Figure out if it's actually a WebGL context.
expect(gl).to.exist;
});
});
});
});
然後我可以運行其餘的測試。
問題
當我運行此通過摸查,一切都很好,因爲核心測試套件是將要運行的第一件事。我擔心的是,如果有任何測試套件在覈心測試套件之前運行,那麼這些測試套件將因內核尚未初始化而失敗。
確保核心測試套件始終在任何其他測試套件之前運行的最佳方法是什麼?
不太什麼我要找的。我需要測試套件在任何其他測試套件之前運行,而不是在測試套件本身之前運行。 –
那麼使用一個測試套件來設置另一個套件的東西並不是一個好的模式。你想把它們分開放在不同的套房裏,但是它們不能相互獨立地運行,這會破壞它們在不同套房中的目的。我會建議去一個不同的方向。但是如果你這樣做,你可以創建一個名爲tests.js和require(「./ suite1.js」)的文件,require(「./ suite2.js」)等等。像JavaScript本身一樣從上到下執行。對不起,第一次沒有正確理解你的問題。 –
這不是一個好的模式,我同意,但這是由於'node-webgl'如何工作而遇到的。我可能會考慮重構我的核心模塊,看看我是否可以在每次測試之後添加某種方法來將其分解,而不必在head-first中運行node-webgl限制。我真的希望避免必須通過'require'手動導入所有測試套件,因爲這既乏味又難以維護。 –