小编ern*_*ejo的帖子

单元测试中的bindToController

我在一个指令中使用bindToController让隔离的作用域直接连接到控制器,如下所示:

app.directive('xx', function () {
  return {
    bindToController: true,
    controller: 'xxCtrl',
    scope: {
      label: '@',
    },
  };
});
Run Code Online (Sandbox Code Playgroud)

然后在控制器中我有一个默认情况下,HTML中没有指定标签:

app.controller('xxCtrl', function () {
  var ctrl = this;

  ctrl.label = ctrl.label || 'default value';
});
Run Code Online (Sandbox Code Playgroud)

如何在Jasmine单元测试中实例化xxCtrl,以便我可以测试ctrl.label?

describe('buttons.RemoveButtonCtrl', function () {
  var ctrl;

  beforeEach(inject(function ($controller) {
    // What do I do here to set ctrl.label BEFORE the controller runs?
    ctrl = $controller('xxCtrl');
  }));

  it('should have a label', function () {
    expect(ctrl.label).toBe('foo');
  });
});
Run Code Online (Sandbox Code Playgroud)

选中此项以测试问题

unit-testing angularjs

30
推荐指数
1
解决办法
5750
查看次数

标签 统计

angularjs ×1

unit-testing ×1