最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to mock a function inside another function (which I am testing) using sinon? - Stack Overflow

programmeradmin4浏览0评论

let's say i have a function

Func a() {
    //Do Something
    let c = b();
    return c;
}

I want to test the function a and mock b() and in the mock want to assign c. Sinon.Stub(Test,"b").returns("DummyValue"); c should be assigned DummyValue.

How can I do that?

describe("a", () => {
    let a = a();
    //mock b();
    action = execute(a);
    expect(action).should.return.("DummyValue");
})

let's say i have a function

Func a() {
    //Do Something
    let c = b();
    return c;
}

I want to test the function a and mock b() and in the mock want to assign c. Sinon.Stub(Test,"b").returns("DummyValue"); c should be assigned DummyValue.

How can I do that?

describe("a", () => {
    let a = a();
    //mock b();
    action = execute(a);
    expect(action).should.return.("DummyValue");
})
Share Improve this question edited Dec 18, 2018 at 10:32 S.. 5,7583 gold badges39 silver badges44 bronze badges asked Feb 9, 2017 at 11:24 Ravi ShankarRavi Shankar 3491 gold badge4 silver badges9 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 7

When we have 2 functions in the same file and want to stub one of them and test the other. For example,: Test: tests.js

let ComputeSumStub = sinon.stub(OfflineLoader, "ComputeSum");
const ans = function ()
{
    return 10;
};
ComputeSumStub.returns(ans);
const actualValue: number = OfflineLoader.sum();
expect(actualValue).to.be.equal(10);

Dev: foo.js

function sum(): number
{
    return ComputeSum(8, 9);
}

function ComputeSum(a: number, b: number): number
{
    return a + b;
}

We cannot do that, because after compilation the functions are exported with different signatures, with full name and while stubbing we stub the global function but while calling it from within the other function, we call the local function, hence it doesn’t work. There is a workaround to do that.

foo.js
const factory = {
  a,
  b,
}
function a() {
  return 2;
}

function b() {
  return factory.a();
}

module.exports = factory;

test.js

const ser = require('./foo');
const sinon = require('sinon');

const aStub = sinon.stub(ser, 'a').returns('mocked return');
console.log(ser.b());
console.log(aStub.callCount);

Ref: Stubbing method in same file using Sinon

You can stub function only

  • if you pass it as parameter ans fake it with test doubles library like sinon
  • or if it is dependency (loaded via import or require). In such case you can use proxyquire to pass in your fake b function for module under test. Function itself can be faked by sinon or other test doubles library.

In this case a sinon stub is more appropriate then a mock When to use mocks vs stubs?

The rule of thumb is: if you wouldn’t add an assertion for some specific call, don’t mock it. Use a stub instead.

Our assertion in the test is not on a specific call of function a i.e 1st or 3rd call but on all calls.

We can tel this because we have programmed our stub to always return the same result regardless of the way in which it is being called (arguments, or number of calls).

Pass a sinon stub function as an argument to function a.

Function a(b) {
    const c = b();

    return c;
}

test.js

require("sinon")

describe("a", () => {
    const stub = sinon.stub();
    stub.returns("DummyValue");
    expect(a(stub)).to.eql.("DummyValue");
})

Note that we can use const for these variable declarations as they are never being reassigned.

发布评论

评论列表(0)

  1. 暂无评论