Sinon: μ—¬λŸ¬ .withArgsλ₯Ό μ‚¬μš©ν•˜μ—¬ sinon.stub()을 μ—°κ²°ν•˜λ©΄ λ§ˆμ§€λ§‰ μ„ μ–Έλ§Œ μŠ€ν…λ©λ‹ˆλ‹€.

에 λ§Œλ“  2012λ…„ 09μ›” 25일  Β·  23μ½”λ©˜νŠΈ  Β·  좜처: sinonjs/sinon

μ—¬λŸ¬ .withArgsλ₯Ό μ‚¬μš©ν•˜μ—¬ sinon.stub()을 μ—°κ²°ν•˜λ©΄ λ§ˆμ§€λ§‰ μ„ μ–Έλ§Œ μŠ€ν…λ©λ‹ˆλ‹€.

var stubbedFunction = sinon.stub().withArgs(argument1).returns(value1).withArgs(argument2).returns(value2);

그런 λ‹€μŒ λ§ˆμ§€λ§‰ μ„ μ–Έ(.withArgs(argument2).returns(value2))만 μ‹€μ œλ‘œ μŠ€ν…λ©λ‹ˆλ‹€.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

이것은 Sinon 1.7.3μ—μ„œ μ œλŒ€λ‘œ μž‘λ™ν•©λ‹ˆλ‹€.

var s = sinon.stub();
s.withArgs(1).returns('a');
s.withArgs(2).returns('b');
s(1); // -> 'a'
s(2); // -> 'b'
s(3) // -> undefined

λͺ¨λ“  23 λŒ“κΈ€

μ‚¬μš© κ°€λŠ₯ν•œ 경우 withArgs λ₯Ό μˆ˜μ •ν•˜κ³  μƒμ„±λœ 원본 μŠ€ν…μ„ λ°˜ν™˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.

+1 이λ₯Ό μœ„ν•΄. κ΄€λ ¨ λ©”λͺ¨μ—μ„œ withArgs κ°€ μΌμΉ˜ν•˜μ§€ μ•ŠλŠ” μΈμˆ˜μ— λŒ€ν•œ μ›λž˜ λ©”μ„œλ“œλ‘œμ˜ 전달을 μ„ νƒμ μœΌλ‘œ ν—ˆμš©ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄, Node.js fs.readFileSync() λ©”μ„œλ“œλ₯Ό μŠ€ν…ν•˜κ³  μ‹Άλ‹€λ©΄ Sinon이 μ›λž˜ κ΅¬ν˜„μ„ μœ μ§€ν•˜μ—¬ require() ( readFileSync μ‚¬μš©)κ°€ μ†μƒλ˜μ§€ μ•Šλ„λ‘ ν•˜κ³ , κ·ΈλŸ¬λ‚˜ λ‚΄ ν…ŒμŠ€νŠΈλ₯Ό μœ„ν•΄ νŠΉμ • νŒŒμΌμ— λŒ€ν•΄ μŠ€ν… 방법을 μ‚¬μš©ν•˜μ‹­μ‹œμ˜€.

이전 λŒ“κΈ€μ˜ 예:

beforeEach(function() {
  global.fs = require('fs');
  sinon.stub(fs, 'readFileSync').onlyWithArgs('my-file.txt').returns('Contents of file');

  // Then require the module under test, which uses fs.readFileSync() internally
  // require() uses original method with correct calling context
  global.myModule = require('../src/my-module');
});

afterEach(function() {
  fs.readFileSync.restore();
});

it('does something with the file', function() {
  expect(myModule.loadFromFile()).toEqual('Contents of file');
});

κ²Œμ‹œλ¬Ό μœ„μ˜ @froots κ°€ λ¬Έμ„œμ˜ μ–΄λ”˜κ°€μ— κ²Œμ‹œλ˜μ–΄ μžˆμŒμ„ μ œμ•ˆν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. require λ₯Ό 톡해 λ‹€λ₯Έ 쒅속성을 ν¬ν•¨ν•˜λŠ” νŒŒμΌμ„ λͺ¨μ˜ν•˜λŠ” 방법에 λŒ€ν•œ 이 μ†”λ£¨μ…˜μ„ μ°ΎκΈ° 전에 μ˜€λž«λ™μ•ˆ κ²€μƒ‰ν–ˆμŠ΅λ‹ˆλ‹€.

쒋은 생각. 그것을 μ°”λŸ¬λ³΄κ³  μ‹ΆμŠ΅λ‹ˆκΉŒ? http://github.com/cjohansen/sinon-web/

이것은 Sinon 1.7.3μ—μ„œ μ œλŒ€λ‘œ μž‘λ™ν•©λ‹ˆλ‹€.

var s = sinon.stub();
s.withArgs(1).returns('a');
s.withArgs(2).returns('b');
s(1); // -> 'a'
s(2); // -> 'b'
s(3) // -> undefined

λ‚˜λŠ” 그것이 μ—¬μ „νžˆ μ—°κ²°λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— 그것이 μž‘λ™ν•˜λŠ” 예라고 μƒκ°ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€(μ›λž˜ μŠ€ν…/λͺ¨μ˜λ₯Ό μ‚¬μš©ν•΄μ•Ό 함).

진싀. κ·ΈλŸ¬λ‚˜ 그것은 μΆ©λΆ„νžˆ μ’‹κ³  무슨 일이 μΌμ–΄λ‚˜κ³  μžˆλŠ”μ§€ λΆ„λͺ…νžˆν•©λ‹ˆλ‹€. 연결은 ν•΄μ„μ˜ 여지λ₯Ό λ‚¨κΉλ‹ˆλ‹€.

@mantoniλ‹˜ 말씀에 λ™μ˜ν•©λ‹ˆλ‹€. μŠ€ν…μ˜ λͺ©μ μ€ μ„œλΉ„μŠ€/쿼리의 계약 응닡을 λ‚˜νƒ€λ‚΄λŠ” κ²ƒμž…λ‹ˆλ‹€. 이 μŠ€νƒ€μΌμ΄ 더 잘 ν‘œν˜„λ©λ‹ˆλ‹€.

λ¬Έμ„œκ°€ μ—¬κΈ°μ—μ„œ κ°œμ„ λ  수 μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

λ‚˜λŠ” λ‹€μŒμ΄ μž‘λ™ν•˜κΈ°λ₯Ό κΈ°λŒ€ν–ˆμŠ΅λ‹ˆλ‹€.

var someObj = {
    someProp: sinon.stub().withArgs("foo").returns(true)
}

λΆ„λͺ…νžˆ μ˜λ„ν•œ λŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 더 μ€‘μš”ν•œ 것은 μ œλŒ€λ‘œ μ‹€νŒ¨ν•˜μ§€ μ•Šμ•˜λ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. foo someObj.someProp true λ₯Ό λ°˜ν™˜ν•˜μ§€ μ•ŠλŠ” λŒ€μ‹ , 항상 true λ₯Ό λ°˜ν™˜ν–ˆμŠ΅λ‹ˆλ‹€. returns μ ˆμ€ Behavior μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜ν•˜λ―€λ‘œ someObj.someProp λŠ” μž…λ ₯에 관계없이 true $λ₯Ό μΆ©μ‹€νžˆ λ°˜ν™˜ν•©λ‹ˆλ‹€.

μ„œλ‘œ λ‹€λ₯Έ μœ ν˜•μ˜ 객체λ₯Ό λ°˜ν™˜ν•˜λŠ” μ—°κ²° ꡬ쑰 이면의 κ·Όκ±°λ₯Ό μ΄ν•΄ν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ‚¬μš©μžλŠ” λ°˜ν™˜λœ κ°œμ²΄κ°€ 무엇인지 잘λͺ» μ•Œκ³  μžˆμŠ΅λ‹ˆλ‹€. CallObjects 및 Behaviorsκ°€ λ…ΈμΆœν•˜λŠ” λ©”μ„œλ“œκ°€ μŠ€ν…μ—μ„œ μ œκ³΅ν•˜λŠ” API와 μΌμΉ˜ν•˜κΈ° λ•Œλ¬Έμ— μŠ€ν…μ΄λΌκ³  κ°€μ •ν•˜λŠ” 이유λ₯Ό μ‰½κ²Œ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

@jasonkarns에 λ™μ˜ν•©λ‹ˆλ‹€. 방금 λ™μΌν•œ "트랩"에 λΉ μ‘ŒμŠ΅λ‹ˆλ‹€. μŠ€ν… 선언을 μΈλΌμΈν•˜λ©΄ μ˜ˆμƒν•œ μŠ€ν… μΈμŠ€ν„΄μŠ€κ°€ λ°˜ν™˜λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ λͺ¨μ˜λ₯Ό μ‚¬μš©ν•  λ•Œ 연결이 μ˜ˆμƒλŒ€λ‘œ μž‘λ™ν•˜λŠ” κ²ƒμœΌλ‘œ λ³΄μž…λ‹ˆλ‹€.

var someObj = {
    someProp: sinon.mock().withArgs("foo").returns(true)
}

μ΄μœ κ°€ μžˆλ‚˜μš”?

λ‚΄κ°€ λ­”κ°€λ₯Ό λ†“μΉ˜κ³  μžˆλŠ”μ§€ ν™•μ‹€ν•˜μ§€ μ•Šμ§€λ§Œ @froots ν¬μΈνŠΈκ°€ μ§€κΈˆ κ°€λŠ₯ν•©λ‹ˆκΉŒ? 이 μž‘μ—…μ„ μˆ˜ν–‰ν•΄μ•Ό ν–ˆμŠ΅λ‹ˆλ‹€.

sinon.stub(fs, 'readdirSync', (dir) => {
    if (dir === 'foo-path') {
        return [
            'my.js',
            'fake.js',
            'stuff.js'
        ];
    }

    return fsReaddir(dir);
});

λ‹€μŒκ³Ό 같은 일을 ν•˜κ³  μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€.

var myStub = sinon.stub(fs, 'readdirSync', fs.readdirSync);

myStub
    .withArgs('foo-path')
    .returns([
        'my.js',
        'fake.js',
        'stuff.js'
    ]);

λ‚˜λŠ” 이것이 μ§€κΈˆ μˆ˜μ •λ˜μ—ˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. μ•„λž˜ μ˜ˆμ œλŠ” [email protected] 둜 μ˜λ„ν•œ λŒ€λ‘œ μž‘λ™ν•©λ‹ˆλ‹€.

var Dummy = {
    doSomething: function(something) {
        return 'doing ...' + something;
    }
}

sinon.stub(Dummy, 'doSomething')
    .withArgs('sleep').returns('sleepy')
    .withArgs('eat').returns('eating');

console.log(Dummy.doSomething('sleep'));
console.log(Dummy.doSomething('eat'));

@valentin-radulescu-hs μ—¬μ „νžˆ 1.17.7 둜 μ‹€νŒ¨ν•©λ‹ˆλ‹€.
withArgs ( @jasonkarns https://github.com/sinonjs/sinon/issues/176#issuecomment-μ—μ„œ μ§€μ ν•œ λŒ€λ‘œ)에 관계없이 μ΅œμ‹  returns λ₯Ό μœ μΌν•œ λ°˜ν™˜ κ°’μœΌλ‘œ μ‚¬μš©ν•©λ‹ˆλ‹€. 78191790).

λ‚˜λ₯Ό μœ„ν•΄ μž‘λ™ν•˜λŠ” 것은 withArgs() λ₯Ό μ—°κ²°ν•˜μ§€ μ•ŠμŒμœΌλ‘œμ¨ @mantoni 의 λ‹΅λ³€ https://github.com/sinonjs/sinon/issues/176#issuecomment -33636496 μž…λ‹ˆλ‹€.

var s = sinon.stub();
s.withArgs(1).returns('a');
s.withArgs(2).returns('b');

@zurfyx λ…Έλ“œ λ˜λŠ” λΈŒλΌμš°μ €μ—μ„œ μ‹€ν–‰ μ€‘μž…λ‹ˆκΉŒ? μ–΄λ–€ λ²„μ „μ˜ λ…Έλ“œ/λΈŒλΌμš°μ €λ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆκΉŒ? 그것이 μ€‘μš”ν•œμ§€ ν™•μ‹€ν•˜μ§€ μ•Šμ§€λ§Œ λ‚˜λŠ” λ‚΄ λμ—μ„œ 볡제λ₯Ό μ‹œλ„ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

μ•ˆλ…•ν•˜μ„Έμš” @valentin-radulescu-hsμž…λ‹ˆλ‹€. babel-cli 6.22.2λ₯Ό μ‚¬μš©ν•˜μ—¬ λ…Έλ“œμ—μ„œ μ‹€ν–‰ μ€‘μž…λ‹ˆλ‹€.

  • λ…Έλ“œ 6.9.4
  • NPM 3.10.10

λŠ¦μ€ λ‹΅λ³€ μ£„μ†‘ν•©λ‹ˆλ‹€

@zurfyx μœ„μ—μ„œ μž‘μ„±ν•œ 것과 λ™μΌν•œ μ½”λ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ–ΈκΈ‰ν•œ κ΅¬μ„±μœΌλ‘œ 볡제λ₯Ό μ‹œλ„ν–ˆμœΌλ©° μ˜ˆμƒλŒ€λ‘œ sleepy 및 eating μ½˜μ†”μ— 좜λ ₯λ©λ‹ˆλ‹€. μ™œ 그것이 λ‹Ήμ‹ μ—κ²Œ 문제λ₯Ό μΌμœΌν‚€λŠ” 지 잘 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€ 😒

@zurfyx _ μ™„μ „νžˆ_ ν™•μ‹€νžˆ ν•˜κΈ° μœ„ν•΄, 이것을 μ‹œλ„ν•΄ λ³΄μ…¨μŠ΅λ‹ˆκΉŒ?

grep version node_modules/sinon/package.json
rm -r node_modules
rm npm-shrinkwrap
npm install
grep version node_modules/sinon/package.json

λŒ€λΆ€λΆ„μ˜ 경우 λ¬Έμ œκ°€ λ°œμƒν•˜λŠ” 것은 μ„€μΉ˜λœ 것과 λ™μΌν•˜μ§€ μ•Šμ„ κ²ƒμœΌλ‘œ μ˜ˆμƒλ˜λŠ” νŒ¨ν‚€μ§€ 버전 λ•Œλ¬Έμž…λ‹ˆλ‹€.

@ fatso83 그것도 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. grep 좜λ ₯ 버전은 μ—¬μ „νžˆ 1.1.7.7 μž…λ‹ˆλ‹€.

이것이 도움이 될 수 μžˆλŠ” κ²½μš°μ— λŒ€λΉ„ν•˜μ—¬ λ‚΄ 전체 ν…ŒμŠ€νŠΈμž…λ‹ˆλ‹€.

  it('test', () => {
    const s = sinon.stub();
    s.withArgs('a').returns('1');
    s.withArgs('b').returns('2');

    console.info(s('c'));
  });

s('a') : '1' βœ“
s('b') : '2' βœ“
s('c') : undefined βœ“

  it('test', () => {
    const s = sinon.stub()
      .withArgs('a').returns('1')
      .withArgs('b').returns('2');

    console.info(s('c'));
  });

s('a') : '2' βœ—
s('b') : '2' βœ“
s('c') : '2' βœ—

sinon.createStubInstance도 μ‚¬μš©ν•˜μ—¬ 이 λ™μž‘μ„ μž¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const stub = sandbox.stub();
      const myObj = function(){};
      const objOne = sinon.createStubInstance(myObj);
      const objTwo = sinon.createStubInstance(myObj);
      stub.withArgs(objOne).returns('1');
      stub.withArgs(objTwo).returns('2');

      console.info(stub(objOne));
      console.info(stub(objTwo));

μ‚°μΆœ:
2 2

λ‚˜λŠ” sinon.createStubInstanceκ°€ μƒˆλ‘œμš΄ μŠ€ν… 객체λ₯Ό 생성해야 ν•œλ‹€κ³  μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€. ν‹€λ¦° λ§μΈκ°€μš”?

이 질문이 이 μŠ€λ ˆλ“œμ— μ†ν•˜λŠ”μ§€ ν™•μ‹€ν•˜μ§€ μ•Šμ§€λ§Œ ν™•μ‹€νžˆ 관련이 μžˆμŠ΅λ‹ˆλ‹€. sinon 1.17.7 κ°€ μžˆλŠ” ν”„λ‘œμ νŠΈμ—μ„œ μ €λŠ” μ—¬λŸ¬ withArgs 문을 μ—°κ²°ν•˜λŠ” 데 μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€. μ—¬κΈ°μ„œ λ§ˆμ§€λ§‰ 문은 κΈ°λ³Έκ°’μœΌλ‘œ μ‚¬μš©λ˜λŠ” "grab-all" λ§€μ²˜μ˜€μŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄:

const s = sinon.stub();
s.withArgs(1).returns(1)
 .withArgs(2).returns(2)
 .withArgs(sinon.match.any).returns('my-default-value')

s(1) // 1
s(5) // my-default-value

이제 4.1.3 λ₯Ό μ‚¬μš©ν•˜κ³  λ™μž‘μ΄ λ³€κ²½λ˜μ–΄ 항상 my-default-value λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. μ˜λ„ν•œ κ±΄κ°€μš”? κ·Έλ ‡λ‹€λ©΄ withArgs μ²΄μΈμ—μ„œ κΈ°λ³Έ/λŒ€μ²΄ 값을 μ •μ˜ν•˜λŠ” 방법이 μžˆμŠ΅λ‹ˆκΉŒ?

λ‚˜λŠ” λ³€ν™”κ°€ μ˜λ„λœ 적이 μ—†λ‹€κ³  μƒκ°ν•œλ‹€. 에 λŒ€ν•œ μˆ˜μ • 사항을 μ œκ³΅ν•  수 μžˆλŠ” 경우
νšŒκ·€ 자유둭게! got을 μ‚¬μš©ν•˜μ—¬ λ°œμƒν•œ μœ„μΉ˜λ₯Ό μΆ”μ ν•˜λŠ” 데 도움을 쀄 수 μžˆμŠ΅λ‹ˆλ‹€.
νƒ“ν•˜λ‹€.

덴맨. 15. 1μ›” 2018, 11.13 skrev David GarcΓ­a [email protected] :

이 질문이 이 μŠ€λ ˆλ“œμ— μ†ν•˜λŠ”μ§€ ν™•μ‹€ν•˜μ§€ μ•Šμ§€λ§Œ
ν™•μ‹€νžˆ 그것과 관련이 μžˆμŠ΅λ‹ˆλ‹€. sinon 1.17.7이 μžˆλŠ” ν”„λ‘œμ νŠΈμ—μ„œ I
μ—¬λŸ¬ withArgs 문을 μ—°κ²°ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. λ§ˆμ§€λ§‰ ν•˜λ‚˜λŠ”
κΈ°λ³Έκ°’μœΌλ‘œ μž‘λ™ν•˜λŠ” "grab-all" 맀처. 예λ₯Ό λ“€μ–΄:

const s = sinon.stub();s.withArgs(1).returns(1)
.withArgs(2).returns(2)
.withArgs(sinon.match.any).returns('λ‚΄ κΈ°λ³Έκ°’')
s(1) // 1s(5) // λ‚΄ κΈ°λ³Έκ°’

이제 4.1.3을 μ‚¬μš©ν•˜κ³  λ™μž‘μ΄ λ³€κ²½λ˜μ—ˆμœΌλ©° 항상 λ°˜ν™˜λ©λ‹ˆλ‹€.
λ‚΄ κΈ°λ³Έκ°’. μ˜λ„ν•œ κ±΄κ°€μš”? κ·Έλ ‡λ‹€λ©΄ μ •μ˜ν•˜λŠ” 방법이 μžˆμŠ΅λ‹ˆκΉŒ?
withArgs 체인의 κΈ°λ³Έ/λŒ€μ²΄ κ°’?

β€”
당신이 μ–ΈκΈ‰λ˜μ—ˆκΈ° λ•Œλ¬Έμ— 이것을 λ°›λŠ” κ²ƒμž…λ‹ˆλ‹€.
이 이메일에 직접 λ‹΅μž₯ν•˜κ³  GitHubμ—μ„œ ν™•μΈν•˜μ„Έμš”.
https://github.com/sinonjs/sinon/issues/176#issuecomment-357638653 λ˜λŠ” μŒμ†Œκ±°
μŠ€λ ˆλ“œ
https://github.com/notifications/unsubscribe-auth/AAluXMzqALJ0JlAM3hUaiK1SSyca9H74ks5tKyS4gaJpZM4AK2eu
.

>

[이미지: --]

μΉΌ 에릭 μ½₯셍
[이미지: https://]about.me/kopseng
https://about.me/kopseng?promo=email_sig&utm_source=email_sig&utm_medium=email_sig&utm_campaign=external_links

@fatso83 λ²”μœ„λ₯Ό μ’ν˜”λŠ”λ° μΌλΆ€λŸ¬ 그랬던 것 κ°™μŠ΅λ‹ˆλ‹€. λ‚˜μ—κ²ŒλŠ” λ§ˆμ§€λ§‰ withArgs λ¬Έμ—μ„œ λŒ€μ²΄λ₯Ό μ§€μ •ν•˜λŠ” 것이 더 μžμ—°μŠ€λŸ½κ²Œ λŠκ»΄μ§€μ§€λ§Œ 이 κ²½μš°μ—λŠ” μˆœμ„œλ₯Ό λ°”κΎΈκ³  .withArgs(sinon.match.any) λ₯Ό 첫 번째 μœ„μΉ˜μ— λ„£μœΌλ©΄ λ¬Έμ œκ°€ ν•΄κ²°λ©λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰