1

I have this simple controller, UserService is a service which return JSON

"use strict";

angular.module("controllers").controller('profileCtrl', ["$scope", "UserService", 
    function ($scope, UserService) {
       $scope.current_user = UserService.details(0);
    }
]);

I can not make the test. However this is my try

'use strict';

describe('profileCtrl', function () {
  var scope, ctrl;

  beforeEach(angular.mock.module('controllers'), function($provide){
    $provide.value("UserService", {
      details: function(num) { return "sdfsdf"; }
    });
  });

  it('should have a LoginCtrl controller', function() {
    expect(controllers.profileCtrl).toBeDefined();
  });

  beforeEach(angular.mock.inject(function($rootScope, $controller){
    scope = $rootScope.$new();
    $controller('profileCtrl', {$scope: scope});
  }));

  it('should fetch list of users', function(){
    expect(controllers.scope.current_user.length).toBe(6);
    expect(controllers.scope.current_user).toBe('sdfsdf');
  });
});
3
  • 1
    You need to explain what exactly is your problem. "It doesn't work" is not good enough. Commented Mar 9, 2014 at 17:23
  • Simply I don't understand the structure of a test, I tried lot of examples but I didn't got it Commented Mar 9, 2014 at 17:29
  • 1
    If you want examples, look here: stackoverflow.com/questions/12509073/angularjs-test-example Commented Mar 9, 2014 at 17:30

1 Answer 1

3

The usage of $controller is correct, that's the way to instantiate a controller for a unit test. You can mock the UserService instance it gets directly in the $controller invocation. You should be using its return value - this is the instance of your controller you're going to test.

You're trying to read stuff from controllers but its not defined anywhere in the test, I guess you're referring to the module.

This is how I would go about it + fiddle

//--- CODE --------------------------
angular.module('controllers', []).controller('profileCtrl', ["$scope", "UserService",

function ($scope, UserService) {
    $scope.current_user = UserService.details(0);
}]);

// --- SPECS -------------------------

describe('profileCtrl', function () {
    var scope, ctrl, userServiceMock;

    beforeEach(function () {
        userServiceMock = jasmine.createSpyObj('UserService', ['details']);
        userServiceMock.details.andReturn('sdfsdf');
        angular.mock.module('controllers');
        angular.mock.inject(function ($rootScope, $controller) {
            scope = $rootScope.$new();
            ctrl = $controller('profileCtrl', {
                $scope: scope,
                UserService: userServiceMock
            });
        });
    });

    it('should have a LoginCtrl controller', function () {
        expect(ctrl).toBeDefined();
    });

    it('should fetch list of users', function () {
        expect(scope.current_user).toBe('sdfsdf');
    });
});

You're welcome to change the fiddle online to see how it affects testing results.

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.