Stub/Assert method on same object as the method beeing tested with Mocha and Sinon

275
April 05, 2017, at 6:39 PM

Simplified I have the following object, where I want to assert that subFunc has been called from func.

export function subFunc(arg) {
  return arg + 'bar';
};
export function func(arg) {
  return subFunc(arg);
};

I have the following test, which fails on both the assertion of subFunc beeing called and the expected returnvalue.

Even though I am stubbing the subFunc, the original method is beeing used.

If the subFunc is on another object than the object beeing tested it works fine.

import * as testObject from '../testObject';
...
it('does not work', () => {
  const subFuncStub = sinon.stub(testObject, 'subFunc');
  subFuncStub.returns('something else');
  const value = testObject.func('foo');
  sinon.assert.called(subFuncStub); // Never called
  expect(value).to.be.equal('something else'); // Is 'foobar'
});

Why is this not working and how can I stub a method on the same object as the method beeing tested?

Answer 1

When you stub, you basically dont call actual function, and check that on calling one function, the stubbed function is called with Args( basically the arguements it expects).

If you are actually looking for functional output, you might not stub it, but mock the data to be passed.

READ ALSO
Randomizing an array inside a ng-Repeat

Randomizing an array inside a ng-Repeat

I have an array of object which I use ng-repeat to iterate overHowever, every object has 2 properties - a country (string) and cities (array)

261
how to transfer element from one drop down to another drop down list in javascript?

how to transfer element from one drop down to another drop down list in javascript?

I don't know the what code I want to write for transfer one drop down lo another can anyone help me?

339
Losing pdf content when using Blob

Losing pdf content when using Blob

I get base64 pdf file from back endWhen try to open that file with Blob I am getting pdf file with empty content

355