Stubbing and/or mocking a class in sinon.js?

后端 未结 2 1515
心在旅途
心在旅途 2021-02-07 07:27

I\'ve created a database wrapper for my application, shown below. To test it, I obviously would like to replace the actual database library. I could create a new class that mock

2条回答
  •  半阙折子戏
    2021-02-07 07:34

    You can use both for that.

    Mock have an expected ordered behavior that, if not followed correctly, is going to give you an error.

    A Stub is a similar to a mock, but without the order, so you can call your methods the way you want. In my experience you almost never need a mock.

    Both of them will substitute your method for an empty method, or a closure if you pass one. It would be something like this:

    stub = sinon.stub(wrapper , 'insertUser ', function () { return true; });
    

    Then you add the expect behavior to check if it did happened.

    I like to use Jasmine with Jasmine-Sinon for checking the tests.

提交回复
热议问题