用茉莉花测试角度"控制器"

Den*_*nis 11 testing bdd jasmine angularjs

我在角应用程序中使用"控制器为"语法.现在是测试的时候了,但所有的例子都是注入$ scope的控制器.我如何调用"this.addItem"方法并检查它是否在Jasmine测试中向"this.items"添加了一个项目?

(function () {
"use strict";
    angular.module('myModule', ['factoryModule'])
    .controller('MyController', function (myFactory) {
        this.items = [];

        this.selectedItem = null;

        this.addItem = function (itemType) {
            var item = myFactory.create(itemType);
            this.items.push(trigger);
            this.selectedItem = item;
        };

        this.removeItem = function (item) {
            this.items.splice(this.items.indexOf(item), 1);
        };
    });
})();
Run Code Online (Sandbox Code Playgroud)

Bra*_*dan 17

只是将@ PSL的评论拉到答案中,这段代码对我有用:

describe('Controller: ExampleController', function () {

  beforeEach(module('app'));

  var ExampleController;

  beforeEach(inject(function ($controller) {
    ExampleController = $controller('ExampleController', {});
  }));

  it('should define foo', function (){
    expect(ExampleController.foo).toBeDefined();
  });
});
Run Code Online (Sandbox Code Playgroud)