Javascript 无法在具有Sinon的导入文件中存根函数调用

Javascript 无法在具有Sinon的导入文件中存根函数调用,javascript,node.js,ecmascript-6,babeljs,Javascript,Node.js,Ecmascript 6,Babeljs,我想声明正在我的通知程序.send函数中调用trackPushNotificationtrackPushNotification和send功能都在同一个文件中运行。 我假设我应该使用Sinon存根trackPushNotification,以便跟踪callCount属性。当我执行测试时,trackPushNotification似乎一点也没有被打断。我搜索了一些东西,显然这与我使用ES6导入/导出的方式有关。我找不到答案,所以我希望有人能帮我解决这个问题 通知程序.send函数如下所示: exp

我想声明正在我的
通知程序.send
函数中调用
trackPushNotification
trackPushNotification和send功能都在同一个文件中运行。
我假设我应该使用Sinon存根
trackPushNotification
,以便跟踪
callCount
属性。当我执行测试时,
trackPushNotification
似乎一点也没有被打断。我搜索了一些东西,显然这与我使用ES6导入/导出的方式有关。我找不到答案,所以我希望有人能帮我解决这个问题

通知程序.send
函数如下所示:

export const send = (users, notification) => {
  // some other logic

  users.forEach(user => trackPushNotification(notification, user));
};
export const trackPushNotification = (notification, user) => {
  return Analytics.track({ name: 'Push Notification Sent', data: notification.data }, user);
};
it('should track the push notifications', () => {
  sinon.stub(notifier, 'trackPushNotification');

  const notificationStub = {
    text: 'Foo notification text',
    data: { id: '1', type: 'foo', track_id: 'foo_track_id' },
  };

  const users = [{
    username: 'baz@foo.com',
  }, {
    username: 'john@foo.com',
  }];

  notifier.send(users, notificationStub);

  assert.equal(notifier.trackPushNotification.callCount, 2);
});
My
notifier.trackPushNotification
函数如下所示:

export const send = (users, notification) => {
  // some other logic

  users.forEach(user => trackPushNotification(notification, user));
};
export const trackPushNotification = (notification, user) => {
  return Analytics.track({ name: 'Push Notification Sent', data: notification.data }, user);
};
it('should track the push notifications', () => {
  sinon.stub(notifier, 'trackPushNotification');

  const notificationStub = {
    text: 'Foo notification text',
    data: { id: '1', type: 'foo', track_id: 'foo_track_id' },
  };

  const users = [{
    username: 'baz@foo.com',
  }, {
    username: 'john@foo.com',
  }];

  notifier.send(users, notificationStub);

  assert.equal(notifier.trackPushNotification.callCount, 2);
});
我的测试用例如下所示:

export const send = (users, notification) => {
  // some other logic

  users.forEach(user => trackPushNotification(notification, user));
};
export const trackPushNotification = (notification, user) => {
  return Analytics.track({ name: 'Push Notification Sent', data: notification.data }, user);
};
it('should track the push notifications', () => {
  sinon.stub(notifier, 'trackPushNotification');

  const notificationStub = {
    text: 'Foo notification text',
    data: { id: '1', type: 'foo', track_id: 'foo_track_id' },
  };

  const users = [{
    username: 'baz@foo.com',
  }, {
    username: 'john@foo.com',
  }];

  notifier.send(users, notificationStub);

  assert.equal(notifier.trackPushNotification.callCount, 2);
});
还做了一个快速测试:

// implementation.js
export const baz = (num) => {
  console.log(`blabla-${num}`);
};

export const foo = (array) => {
  return array.forEach(baz);
};

// test.js
it('test', () => {
  sinon.stub(notifier, 'baz').returns(() => console.log('hoi'));

  notifier.foo([1, 2, 3]); // outputs the blabla console logs

  assert.equal(notifier.baz.callCount, 3);
});

这是测试它的一种方法。请注意,调用trackPushNotification时需要语句

模块: 测试时导入
谢谢,调用函数时添加
this
的提示帮了我的忙,没有它,sinon似乎找不到它应该使用的方法:)