Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock dependencies #51

Open
joyarzun opened this issue Jan 28, 2018 · 1 comment
Open

Mock dependencies #51

joyarzun opened this issue Jan 28, 2018 · 1 comment

Comments

@joyarzun
Copy link

Hi
My code looks like:

const myApi = require("./myApi");

robot.respond(/my regex/i,  res => {
      myApi
        .process()
        .then(result => res.send(result.message))
});

myApi is a wrapper for Google Api. So, I want to test mocking my Api. I usually use proxyquire but I don't know because you only pass the path and don't do a require. Do you have any clue?

@ghost
Copy link

ghost commented May 19, 2018

I was able to mock a dependency of my script by using mockery and sinon. Here's a snippet that shows an example:

const mockery = require('mockery');
const sinon = require('sinon');
const Helper = require('hubot-test-helper');
const co = require('co');

const { expect } = require('chai');

const processFn = sinon.stub();
const mockApi = {
  process: processFn
};

describe('script', () => {
  beforeEach(() => {
    const helper = new Helper('./script.js');
    mockery.registerMock('./myApi'.js, mockApi);
    mockery.enable({ warnOnUnregistered: false });
    this.room = helper.createRoom({ httpd: false });
  });

  afterEach(() => {
    this.room.destroy();
    processFn.reset();
    mockery.disable();
  });

  context('script', () => {
    beforeEach(() => {
      processFn.resolves('Hello!');
      return co(function * () {
        yield this.room.user.say('brian', '@hubot my regex');
      }.bind(this));
    });

    it('returns', () => {
      expect(this.room.messages).to.equal([
        ['brian', '@hubot my regex'],
        ['hubot', 'Hello!']
      ]);
    });
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant