저는 실제로보고있는 동작에 혼란 스럽습니다. 나의 간첩은 논쟁을 잘못보고하고있다. 똑같은 서명과 인자를 가진 동일한 함수를 만들고 따로 따로 스파이를 작성하면 복사본이 정상적으로 작동합니다. 내가 무슨 일인지 알아낼 수 없습니다!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!
여기에 무슨 일이 일어날 지 모르겠습니까?
모두 감사합니다. 미리 감사드립니다!