2017-04-26 58 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仔细检查它。

相关问题