Testing angular "controller as" with jasmine

Denis

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);
        };
    });
})();
Brandan

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();
  });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related