Organize AngularJS Tests within a test file


    var $controller;
    var calculatorService;
    
    beforeEach(function () {

        // load module
        module('app');

        // overrides for mock injections
        module(function ($provide) {
            // override any dependency here
            // $provide.value('service', 'override'); 

        });
        
        // initialise
        inject(function(_$controller_, _calculatorService_) {
            $controller = _$controller_;
            calculatorService = _calculatorService_;
            
        });

    });
    it('should initialise the controller successfully', function() {

        // act
        var result = $controller('homeController');

        // assert
        expect(result).toBeDefined();

    });

    describe('when add is performed', function() {
        
        it('should invoke add in calculatorService', function() {

            // arrange
            // replace original method with spy
            calculatorService.add = jasmine.createSpy('add');
            var target = $controller('homeController', { calculatorService: calculatorService });

            // act
            target.add(1, 2);

            // assert
            expect(calculatorService.add).toHaveBeenCalledWith(1, 2);

        });
    calculatorService.add = jasmine.createSpy('add');
    var target = $controller('homeController', { calculatorService: calculatorService });
       // overrides for mock injections
        module(function ($provide) {
            $provide.value('claculatorService', 'mockCalculatorService'); 

        });
comments powered by Disqus