Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing angular "controller as" with jasmine

I'm using "controller as" syntax in angular app. And now it's time for testing, but all the examples are for controllers that are injected with $scope. How do i call "this.addItem" method and check that it added an item to "this.items" in Jasmine test?

(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);
        };
    });
})();
like image 694
Denis Avatar asked Sep 18 '14 21:09

Denis


1 Answers

Just to pull @PSL's comment into an answer, this code worked for me:

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();
  });
});
like image 126
Brandan Avatar answered Sep 28 '22 22:09

Brandan