2017-09-22 95 views
2

我有一個Redux動作,它本身分派兩個其他動作。每個操作都從導入的函數中檢索。一個來自本地模塊,另一個來自外部庫。無法使用Sinon存根功能

import { functionA } from './moduleA'; 
import { functionB } from 'libraryB'; 

export function myAction() { 
    return (dispatch) => { 
    dispatch(functionA()); 
    ... 
    dispatch(functionB()); 
    } 
} 

在我的測試我使用的是sinon沙箱存根出的功能,但只有兩個測試通過。我期待所有3通過。

import * as A from './moduleA'; 
import * as B from 'libraryB'; 

describe(__filename, async() => { 
    it('Calls 2 other actions',() => { 
    sandbox = sinon.sandbox.create(); 

    const dispatch = sandbox.stub(); 

    sandbox.stub(A, 'functionA'); 
    sandbox.stub(B, 'functionB'); 

    await myAction()(dispatch); 

    // passes 
    expect(dispatch.callCount).to.equal(2); 

    //passes 
    expect(A.functionA).to.have.been.called(); 

    // fails 
    expect(B.functionB).to.have.been.called();  
    }); 
}); 

最後想到失敗,出現錯誤:

TypeError: [Function: functionB] is not a spy or a call to a spy!

當我輸出的功能到控制檯我得到這個,這似乎與通天塔導入導出出口 的方式(ES6 re-exported values are wrapped into Getter )。這些功能可以正常工作,而不是在測試中。

{ functionA: [Function: functionA] } 
{ functionB: [Getter] } 

我一直在使用stub.get(getterFn)嘗試,但只是給我的錯誤:

TypeError: Cannot redefine property: fetchTopicAnnotations

回答

0

你試過命名您的存根?你的代碼有點奇怪。你在測試中的任何一點都不是指你的存根。

import * as A from './moduleA'; 
import * as B from 'libraryB'; 

describe(__filename, async() => { 
    it('Calls 2 other actions',() => { 
    sandbox = sinon.sandbox.create(); 

    const dispatch = sandbox.stub(); 

    const functionAStub = sandbox.stub(A, 'functionA'); 
    const functionBStub = sandbox.stub(B, 'functionB'); 

    await myAction()(dispatch); 

    // passes 
    expect(dispatch.callCount).to.equal(2); 

    //passes 
    expect(functionAStub.called).toBe(true); 

    // fails 
    expect(functionBStub.called).toBe(true);  
    }); 
}); 
+0

感謝您的回答,但命名存根沒有任何影響 –