2016-03-29 45 views
3

背景如何定義的順序

我工作中的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; 
      }); 
     }); 
    }); 
}); 

然後我可以運行其餘的測試。

問題

當我運行此通過摸查,一切都很好,因爲核心測試套件是將要運行的第一件事。我擔心的是,如果有任何測試套件在覈心測試套件之前運行,那麼這些測試套件將因內核尚未初始化而失敗。

確保核心測試套件始終在任何其他測試套件之前運行的最佳方法是什麼?

回答

2

在我重構我的代碼,以允許核心模塊年底被拆除,而不會影響node-webgl,並使用before塊來初始化它,就像這樣:

// Run this before all tests to ensure node-webgl is initialized 
before(function() { 
    if (!global._document) { 
     global._document = WebGL.document(); 
     global._document.setTitle('Machination Graphics Test Suite'); 
    } 
    if (!global._canvas) { 
     global._canvas = global._document.createElement('canvas', 640, 480); 
    } 
}); 

describe('test suite goes here', function() { 
    // Ensure core is ready for us before testing (except when testing core) 
    before(function() { 
     if (!core.isInitialized()) { 
      core.init(global._canvas); 
     } 
    }); 
    // Tear down core after all tests are run 
    after(function() { 
     core.deinit(); 
    }); 
    ... 
}); 
0

在他們的文檔中描述之前使用()。

describe('hooks', function() { 
    before(function() { 
     // runs before all tests in this block 
    }); 
    ...... 
}); 

之前的函數會先運行,其他所有函數都會在它之後運行。

希望這有助於。

+0

不太什麼我要找的。我需要測試套件在任何其他測試套件之前運行,而不是在測試套件本身之前運行。 –

+1

那麼使用一個測試套件來設置另一個套件的東西並不是一個好的模式。你想把它們分開放在不同的套房裏,但是它們不能相互獨立地運行,這會破壞它們在不同套房中的目的。我會建議去一個不同的方向。但是如果你這樣做,你可以創建一個名爲tests.js和require(「./ suite1.js」)的文件,require(「./ suite2.js」)等等。像JavaScript本身一樣從上到下執行。對不起,第一次沒有正確理解你的問題。 –

+0

這不是一個好的模式,我同意,但這是由於'node-webgl'如何工作而遇到的。我可能會考慮重構我的核心模塊,看看我是否可以在每次測試之後添加某種方法來將其分解,而不必在head-first中運行node-webgl限制。我真的希望避免必須通過'require'手動導入所有測試套件,因爲這既乏味又難以維護。 –