Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| development:testing:sinonjs:start [2019/01/13 12:42] – [Sinon JS] don.lee | development:testing:sinonjs:start [2019/01/13 13:05] (current) – don.lee | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Sinon JS ====== | ====== Sinon JS ====== | ||
| + | |||
| + | If your test will also involve other 3rd party library, etc. multer. You better also check their own test cases for reference. e.g. https:// | ||
| ===== Reference ===== | ===== Reference ===== | ||
| Line 21: | Line 23: | ||
| * Replace existing method with stub to avoid actually execute the method | * Replace existing method with stub to avoid actually execute the method | ||
| + | |||
| + | ==== Method 1 ==== | ||
| + | |||
| + | * define a fake function so that it will call that fake one when call | ||
| < | < | ||
| function fake() { // a fake function to be called | function fake() { // a fake function to be called | ||
| } | } | ||
| + | |||
| const stub = sinon.stub(object, | const stub = sinon.stub(object, | ||
| ... | ... | ||
| + | stub.restore() // remove stub | ||
| + | expect(stub.callCount).to.be.eql(n) // check if the method is call n times | ||
| + | </ | ||
| + | |||
| + | ==== Method 2 ==== | ||
| + | |||
| + | * define a return value no matter how you call the stub | ||
| + | |||
| + | < | ||
| + | const stub = sinon.stub(object, | ||
| + | ... | ||
| + | const test = stub() | ||
| + | expect(test).to.be.eql(val) | ||
| + | stub.restore() // remove stub | ||
| + | expect(stub.callCount).to.be.eql(n) // check if the method is call n times | ||
| + | </ | ||
| + | |||
| + | ==== Method 3 ==== | ||
| + | |||
| + | * resolve a promise with same value no matter how you call the stub | ||
| + | |||
| + | < | ||
| + | const stub = sinon.stub(object, | ||
| + | ... | ||
| + | const test = await stub() | ||
| + | expect(test).to.be.eql(val) | ||
| stub.restore() // remove stub | stub.restore() // remove stub | ||
| expect(stub.callCount).to.be.eql(n) // check if the method is call n times | expect(stub.callCount).to.be.eql(n) // check if the method is call n times | ||
| </ | </ | ||