2017-10-19 168 views
2

我有一個模塊實例化導入的類並異步調用這些實例的方法。每個測試實例的模擬依賴關係類

如何在每個測試用例中獨立地模擬這些方法,以便這些mock只有在測試用例內部創建的情況下才有意義,因爲我無法在測試結束時可靠地恢復mock?

例子:

// tested class 
import B from './b'; 
import C from './c'; 

export default class A { 
    someFunction() { 
    let instanceB = new B(); 
    return instanceB.doSomething() 
     .then(() => this.doSomethingElse()) 
     .then((data) => { 
     // async context, other tests will start before this. 
     let instanceC = new C(data); 
     }); 
    } 
} 

// test 
import A from './a'; 
describe('test',() => { 
    it('case1',() => { 
    a = new A(); 
    // Mock B, C with config1 
    return a.someFunction().then(() => {/* asserts1 */ }); 
    }) 
    it('case2',() => { 
    a = new A(); 
    // Mock B, C with config2 
    return a.someFunction().then(() => {/* asserts2 */ }); 
    }) 
}) 

如果我嘲笑的情形1 B和C同步恢復它們,因爲案例2前的C在異步上下文中實例化運行C'S配置將被覆蓋。 出於同樣的原因,我無法在asserts1之後異步恢復mock。

也有類似的問題:Stubbing a class method with Sinon.js,How to mock dependency classes for unit testing with mocha.js? 但他們沒有涵蓋異步嘲笑的問題。

回答

0

所以我結束了(不漂亮)構造函數注入。如果你有更好的方法,包括測試和編寫異步工廠的完全不同的方法,請分享,我很樂意接受

// tested class 
import B_Import from './b'; 
import C_Import from './c'; 

let B = B_Import; 
let C = C_Import; 
export function mock(B_Mock, C_Mock) { 
    B = B_Mock || B_Import; 
    C = C_Mock || C_Import; 
} 

export default class A { 
    someFunction() { 
    let instanceB = new B(); 
    return instanceB.doSomething() 
     .then(() => this.doSomethingElse()) 
     .then((data) => { 
     // async context, other tests will start before this. 
     let instanceC = new C(data); 
     }); 
    } 
} 


// test 
import A, { mock as mockB } from './a'; 

setupMockB = (cfg, mockCallback) => { 
    const ClassMock = class { 
     constructor() { 
      // use cfg 
     } 
     doSomething() {} 
    } 
    if(mockCallback) {mockCallback(ClassMock.prototype);} 
    mockB(ClassMock, null) 
} 

describe('test',() => { 
    afterEach(() => mockB()) 
    it('case1',() => { 
    a = new A(); 
    setupMockB(cfg1, (mb) => sinon.stub(mb, 'doSomething').resolves()) 
    return a.someFunction().then(() => { /* asserts1 */ }); 
    }) 
    it('case2',() => { 
    a = new A(); 
    setupMockB(cfg2, (mb) => sinon.stub(mb, 'doSomething').rejects()) 
    return a.someFunction().then(() => { /* asserts2 */ }); 
    }) 
})