发布于 2015-09-28 14:07:01 | 208 次阅读 | 评论: 2 | 来源: PHPERZ

下面是“创建AngularJS服务”一章中“依赖”例子里的“notify”服务的单元测试。测试用使用了Jasmine spy替代了真实浏览器的alert。formatDate

var mock, notify;

beforeEach(function() {
  mock = {alert: jasmine.createSpy()};

  module(function($provide) {
    $provide.value('$window', mock);
  });

  inject(function($injector) {
    notify = $injector.get('notify');
  });
});

it('should not alert first two notifications', function() {
  notify('one');
  notify('two');

  expect(mock.alert).not.toHaveBeenCalled();
});

it('should alert all after third notification', function() {
  notify('one');
  notify('two');
  notify('three');

  expect(mock.alert).toHaveBeenCalledWith("one\ntwo\nthree");
});

it('should clear messages after alert', function() {
  notify('one');
  notify('two');
  notify('third');
  notify('more');
  notify('two');
  notify('third');

  expect(mock.alert.callCount).toEqual(2);
  expect(mock.alert.mostRecentCall.args).toEqual(["more\ntwo\nthird"]);

相关主题

  • 理解AngularJS服务
  • 创建AngularJS服务
  • 管理服务依赖
  • 将AngularJS注入到控制器

相关API

Angular Service API

最新网友评论  共有(2)条评论 发布评论 返回顶部
PHPERZ网友 发布于2015-09-29 04:19:03
AngularJS真是个好东西啊,
  • 2楼  PHPERZ网友 回复于2015-09-29 04:19:18
    顶你
  • 支持(0)  反对(0)  回复

    Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务