ste
ste

Reputation: 3269

Problem stubbing a class method with Sinon

I have a NodeJS + Typescript application with the following class:

export default class OrderStreamWriter {
  private readonly redis: IORedis;
  private readonly orderStream: string;
  private readonly logger: LoggerFactory;

  constructor(redisHost: string, redisPort: number, redisPass: string, orderStream: string) {
    this.orderStream = orderStream;

    this.redis = createRedisClient(redisHost, redisPort, redisPass);
    this.logger = new LoggerFactory('streams/OrderStreamWriter');
  }

  public async write(msg: string): Promise<void> {
    await this.redis.xadd(this.orderStream, '*', 'trade', msg).catch((err: Error) => {
      this.logger.log(
        `Error Writing message to stream (${this.orderStream}): ${err.message}. Quitting...`,
      );
      process.exit(1);
    });
  }
}

In another class I use the write method to write the result in a Redis stream. I want to test that flow without calling the actual write function but just to check that that function will be called with certain parameters, here's my test(run using mocha + sinon):

  it('process the input and return an order', () => {
    const rule = directOrder[0].rule;
    const user = directOrder[0].user;
    //const writeStub = sinon.stub(OrderStreamWriter.prototype, "write");
    const Writer: any = sinon.stub();
    sinon.stub(Writer.prototype, "write");
    const writer = new Writer();
    const order = {}

    // console.log(writeStub)
    const directTriggerStrategy: TriggerContext = new TriggerContext(user, rule, writer);
    directTriggerStrategy.execute()

    sinon.assert.calledWithExactly(writer, order);
  })

With both the current code and the commented line const writeStub = sinon.stub(OrderStreamWriter.prototype, "write"); I receive the same error when running the test:

TypeError: Cannot stub non-existent property write

How can I fix this?

Upvotes: 0

Views: 876

Answers (1)

evolveShishir
evolveShishir

Reputation: 11

// someclass.js

class SomeClass {
  prop1;
  prop2;
  contructor(param1, param2) {
    this.prop1 = param1;
    this.prop2 = param2;
  }

  async someFunction(givenParam) {
    // do something here

    return "somedata";
  }
}

// create a factory . This is what you will use to create an instance rather than using the new word ever time to create a new instance.

const someClassInstanceFactory = (param1, param2) => {
  return new SomeClass(param1, param2);
};

export default { someClassInstanceFactory, SomeClass };

// ********************************************************
// somemodule.js

// this file uses the class we created above as below

import classAndFactory from "./someclass.js";

const moduleFunction = () => {
  const instance = classAndFactory.someClassInstanceFactory(param1, param2);
  // this line returns an instance of SomeClass . it's like calling new SomeClass(pram1, param2);

  const result = instance.someFunction("givenParam");
  console.log(result);

  return result;
  // result should be 'somedata'

  // so how do we unit test moduleFunction and stub instance.someFunction when testing? ?
};

export default moduleFunction;

// *******************************************************************

// somemodule.test.js

import classAndFactory from "./../someclass.js";
import moduleFunction from "./somemodule.js";

const { someClassInstanceFactory, SomeClass } = classAndFactory;

// now if you want to stub any thing what you do is

describe("moduleFunction", () => {
  const fakeSomeClassInstance = new SomeClass(1, 2);

  // arrange

  // stub the factory first to return your fake instance created above.

  const expectedResut = "stubbed yoo";
  sinon
    .stub(classAndFactory, "someClassInstanceFactory")
    .returns(fakeSomeClassInstance);

  someFunctionStub = sinon
    .stub(fakeSomeClassInstance, "someFunction")
    .returns(expectedResut);

  it("must call someFunction function with required argument", () => {
    // act

    const result = moduleFunction();

    // assert
    sinon.assert.calledOnce(someFunctionStub);
    assert.equals(result, expectedResut);
  });
});

Upvotes: 1

Related Questions