jasmine + angular ex 4

by ronapelbaum

HTML

<script src="https://jasmine.github.io/2.4/lib/jasmine.js"></script>
<script src="https://jasmine.github.io/2.4/lib/jasmine-html.js"></script>
<script src="https://jasmine.github.io/2.4/lib/boot.js"></script>
<link rel="stylesheet" href="https://jasmine.github.io/2.4/lib/jasmine.css">
<script src="https://code.angularjs.org/1.4.9/angular.js"></script>
<script src="https://code.angularjs.org/1.4.9/angular-mocks.js"></script>

JavaScript

//--------------BL------------
(function() {
    function MyDataService() {
        this.getNames = function() {
            return ['Alice', 'Bob'];
        }
    }

    function MyController(MyDataService) {
        this.greetings = [];
        this.init = function() {
            var names = MyDataService.getNames();
            for (var i = 0; i < names.length; i++)
                var greet = 'Hello ' + names[i];
            this.greetings.push(greet);
        }
    }

    angular.module('MyModule', [])
        .service('MyDataService', MyDataService)
        .controller('MyController', ['MyDataService', MyController]);

})();


//--------------specs------------
describe("MyController test suite", function() {
    var ctrl;
    beforeEach(module('MyModule'));

    beforeEach(inject(function($controller) {
        // The injector unwraps the underscores (_) from around the parameter names when matching
        ctrl = $controller("MyController");
    }));

    it("should have greetings array", function() {
        expect(ctrl.greetings).toBeDefined();
    });
    
    xit("init() should update greetings array", function() {
    	//TODO fix test
        ctrl.init();
        expect(ctrl.greetings.length).toBe(2);
    });


});