【问题标题】:What's the equivalent of jasmine.createSpy().and.callFake(fn) in sinonjssinonjs 中 jasmine.createSpy().and.callFake(fn) 的等价物是什么
【发布时间】:2017-06-06 08:13:19
【问题描述】:

我在 sinonjs 中寻找相当于 jasmine.createSpy().and.callFake(fn) 的东西。

例如:

const mySpy = jasmine.createSpy('my spy')
.and
.callFake((options) => Object.assign({}, {name: 'foo'}, options));

【问题讨论】:

    标签: javascript unit-testing sinon jasmine2.0 sinon-chai


    【解决方案1】:

    可以修改(可选包装函数的)返回值的间谍在 Sinon 用语中称为 stub,因此您正在寻找 is the documentation on stubs。您的示例如下所示:

    const myStub = sinon.stub().callsFake((options) => Object.assign({}, {name: 'foo'}, options));
    
    console.log(myStub().name === 'foo') // => 'true'
    

    披露:我是 Sinon 维护团队的一员。

    【讨论】:

      【解决方案2】:

      根据我从 Jasmine 文档中了解到的情况,这应该做类似的事情:

      const mySpy = sinon.spy((options) => Object.assign({}, {name: 'foo'}, options))
      

      【讨论】:

        【解决方案3】:

        这应该有效:

        var stub = sinon.stub(object, "method", func);
        

        看看这个:http://legacy.sinonjs.org/docs/

        【讨论】:

        • 该方法签名自版本 2 以来已被弃用。您现在使用 callsFake 执行相同操作:sinonjs.org/releases/v2.3.4/stubs。否则很好,但请记住,提问者的问题中没有对象也没有方法。
        猜你喜欢
        • 1970-01-01
        • 2014-05-08
        • 2014-06-12
        • 1970-01-01
        • 2022-11-28
        • 2021-06-19
        • 2012-07-20
        • 2023-03-30
        • 2011-04-08
        相关资源
        最近更新 更多