心跳监视器并在 JavaScript 中对其进行单元测试 - 如何?

发布于 2025-01-08 12:26:02 字数 1154 浏览 1 评论 0原文

我有一个“服务模块”,它监听周期性的心跳(通过socket.io),然后如果错过心跳,就会在事件总线(Backbone.Events)上发布一个事件。此外,如果心跳稍后恢复,它也会发布一个事件。

我的单元测试看起来像这样:

describe('Heartbeat service', function() {
  var HeartbeatService;
  var heartbeatInterval = 1;
  //the server is expected to send heartbeats FASTER actually.
  //otherwise we risk false alarms

  jasmine.require(['services/HeartbeatService'], function(Service) {
    HeartbeatService = Service;
  });

  /*Since these tests use setInterval, it'd be erroneous to allow
    mocks to be overwritten by another test. Thus not using beforeEach()*/
  function createMocks(){
    var mockEventAggregator = jasmine.createSpyObj('Events',['trigger']);
    return {
      target : new HeartbeatService(heartbeatInterval, mockEventAggregator),
      ea     : mockEventAggregator
    };
  }

  it('should raise an event on the FIRST missed beat', function() {
    var mocks = createMocks();
    mocks.target.start();
    setTimeout(function(){
      expect(mocks.ea.trigger).toHaveBeenCalled();
    }, 2);
  });
  //..other similar tests
});

我不喜欢这个测试是,如果它失败了,那么报告工具很可能会列出不同测试下的失败 - 因为实际的断言将发生在 it() 函数之外。

I have a 'service module' that listens for periodic heartbeats (over socket.io) and then if a heartbeat is missed publishes an event on an event bus (Backbone.Events). Also if the heart beat resumes later, it publishes an event.

My unit test looks something like this:

describe('Heartbeat service', function() {
  var HeartbeatService;
  var heartbeatInterval = 1;
  //the server is expected to send heartbeats FASTER actually.
  //otherwise we risk false alarms

  jasmine.require(['services/HeartbeatService'], function(Service) {
    HeartbeatService = Service;
  });

  /*Since these tests use setInterval, it'd be erroneous to allow
    mocks to be overwritten by another test. Thus not using beforeEach()*/
  function createMocks(){
    var mockEventAggregator = jasmine.createSpyObj('Events',['trigger']);
    return {
      target : new HeartbeatService(heartbeatInterval, mockEventAggregator),
      ea     : mockEventAggregator
    };
  }

  it('should raise an event on the FIRST missed beat', function() {
    var mocks = createMocks();
    mocks.target.start();
    setTimeout(function(){
      expect(mocks.ea.trigger).toHaveBeenCalled();
    }, 2);
  });
  //..other similar tests
});

My dislike for this test is that if it fails then it is likely that the reporting tool will list the failures under a different test - since the actual assertion will occur outside the it() function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凯凯我们等你回来 2025-01-15 12:26:02

您可以使用SinonJSs fake计时器将时钟向前设置2个刻度,而不是使用setTimeout。

You could use SinonJSs fake timer to set the clock 2 ticks forward instead of using setTimeout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文