3

Trying to get some units tests in AngularJS (using jasmine & karma) working and struggling to comprehend dependency injection... current error message in karma reads 'Error: Argument 'fn' is not a function, got string'

app.js

angular.module('App', [ 'App.Services', 'App.Controllers', 'App.Directives']);

controller.js

angular.module('App.Controllers', []).
controller('MarketplaceCtrl', function ($scope, apiCall) {
    apiCall.query({
        type: 'engagement',
        engagement_status__in: '0,1'
    }, function(data) {
        var engagements = {};
        $.each(data.objects, function (i, engagement) {
           engagements[engagement.lawyer_id] = engagement
        });
        $scope.engagements = engagements;
    });
});

services.js

angular.module('App.Services', ['ngResource']).
factory('apiCall', function ($resource) {
    return $resource('/api/v1/:type',
        {type: '@type'},
        {
            query: {
                method: 'GET',
                isArray: false
            }
        }
    );
});

controllerSpec.js

describe('controllers', function () {

    beforeEach(
        module('App', ['App.Controllers', 'App.Directives', 'App.Services'])
    );

    describe('MarketplaceCtrl', function () {
        var scope, ctrl, $httpBackend;

        beforeEach(inject(function (_$httpBackend_, $rootScope, $controller) {
            $httpBackend = _$httpBackend_;
            $httpBackend.expectGET('/api/v1/engagement?engagement_status__in=0,1').
                respond([]);
            scope = $rootScope.$new();
            /* Why is MarketplaceCtrl not working? :( */
            ctrl = $controller('MarketplaceCtrl', {$scope: scope});
        }));

        it('should have a MarketplaceCtrl controller', (function () {
            expect(ctrl).not.to.equal(null);
        }));
    });
});
1
  • i run into same error. I am writing tests for a controller. This is in it's own file. So i include the controller script into test jasmine tests page, So do i require the app.js also to be included into the same for writing tests to my controller? Could you also include the scripts you included to write the tests Commented Aug 28, 2013 at 7:24

1 Answer 1

3

Ended up using this example https://github.com/tebriel/angular-seed/commit/b653ce8e642ebd3e2978d5404db81897edc88bcb#commitcomment-3416223

Basically:

describe('controllers', function(){
    beforeEach(module('myApp.controllers'));

    it('should ....', inject(function($controller) {
        //spec body
        var myCtrl1 = $controller('MyCtrl1');
        expect(myCtrl1).toBeDefined();
    }));

    it('should ....', inject(function($controller) {
        //spec body
        var myCtrl2 = $controller('MyCtrl2');
        expect(myCtrl2).toBeDefined();
    }));
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.