2017-04-26 15 views
0

我真的很困惑,我看到的行爲。我的間諜一直誤報論據。如果我使用相同的簽名和參數創建相同的函數並分別監視它,則複製工作正常。我不能弄清楚發生了什麼事!Sinon間諜失敗與一個功能的壞參數,但成功爲另一個相同的

所以,這裏是代碼:

NB:alert是原始功能,test是新的我創建檢查發生了什麼事情。

# index.js 
class Alerter { 

    getOnErrorFn(name) { 
    return (error) => { 
     ... 
     alert = { 
     opts: { 
      tags: 'Unknown Error' 
     } 
     } 
     ... 

     if (alert) { 
     this.test(name, error.message, Object.assign({}, alert.opts, {smc_error: error.toLog()}), alert.level); 
     this.alert(name, error.message, Object.assign({}, alert.opts, {smc_error: error.toLog()}), alert.level);   } 
    }; 
    } 

    test(serviceName, message, opts={}, level=this.CRITICAL) { 
    console.log(serviceName, message, opts, level); 
    } 

    alert(serviceName, message, opts={}, level=this.CRITICAL) { 
    console.log(serviceName, message, opts, level); 
    ... 
    } 

這裏是我的測試代碼(所有其他測試被註釋掉了,這是在測試套件的唯一文件);

# alerter.spec.js 
const sandbox = sinon.sandbox.create(); 

describe('Alerter', function(){ 

    let alerter; 
    let name = 'test-service'; 
    let message = 'test message'; 

    beforeEach(() => { 
    alerter = new Alerter(name); 
    }); 

    afterEach(() => { 
    sandbox.restore(); 
    }); 

    describe('getOnErrorFn()',() => { 
    it.only('handles non-SMCError errors and assumes they should be alerted',() => { 
     const spy = sandbox.spy(alerter, 'test'); 
     const spi = sandbox.spy(alerter, 'alert'); 
     const onError = alerter.getOnErrorFn(name); 

     const error = new Error(); 
     const smcError = SMCError.from(error); 
     onError(error); 
     expect(spy).to.have.been.calledWith(name, smcError.message, {smc_error: smcError.toLog(), tags: 'Unknown Error'}, undefined); 
     expect(spi).to.have.been.calledWith(name, smcError.message, {smc_error: smcError.toLog(), tags: 'Unknown Error'}, undefined); 
    }); 

    }); 

}); 

這裏是測試運行的結果....這是我的絕對堅果!

$ npm test 

> [email protected] test /Users/al/Studio/Projects/smc/app/smc-alerting 
> mocha test/**/*.spec.js 



    Alerter 
    getOnErrorFn() 
TEST test-service Caught Error { tags: 'Unknown Error', 
    smc_error: 'Error Caught Error\n caused by: Caught Error' } critical 
ALERT test-service Caught Error { tags: 'Unknown Error', 
    smc_error: 'Error Caught Error\n caused by: Caught Error' } critical 
     1) handles non-SMCError errors and assumes they should be alerted 


    0 passing (34ms) 
    1 failing 

    1) Alerter getOnErrorFn() handles non-SMCError errors and assumes they should be alerted: 
    expected alert to have been called with arguments test-service, Caught Error, { 
    smc_error: "Error Caught Error 
    caused by: Caught Error", 
    tags: "Unknown Error" 
}, undefined 
    alert(test-service, Caught Error, { smc_error: "Error Caught Error 
    caused by: Caught Error" }, undefined) at /Users/al/Studio/Projects/smc/app/smc-alerting/src/index.js:46:14 
    AssertionError: expected alert to have been called with arguments test-service, Caught Error, { 
    smc_error: "Error Caught Error 
    caused by: Caught Error", 
    tags: "Unknown Error" 
    }, undefined 
     alert(test-service, Caught Error, { smc_error: "Error Caught Error 
    caused by: Caught Error" }, undefined) at src/index.js:46:14 
     at Context.it.only (test/index.spec.js:173:32) 



npm ERR! Test failed. See above for more details. 

所以,注意,這兩個console.log報表打印相同結果。但是,alert函數的間諜會失敗,並顯示一個打印輸出,指示使用第三個參數中缺少的tags屬性調用該函數。 WTF!

還是有什麼我不知道在這裏發生?

所有幫助非常感謝。提前致謝!

回答

0

達尼特。得到了答案。長話短說:使用不可變對象!

alert()有一個delete opts.tags行後面的代碼,這當然會改變原來的對象,當時sinon仔細檢查它。

相關問題