jeudi 2 juillet 2015

Undefined is not a function/not an object

I'm trying to do some unit testing on my Ionic app, using the Karma-jasmine framework, and it's giving me quite the headache. I have a function in my controller I want to test by giving it some dummy input (the credentials), but I keep getting the errors

TypeError: 'undefined' is not a function (evaluating '$controller')
TypeError: 'undefined' is not an object (evaluating '$scope.credentials.username = "uname"')

I've tried some alternate ways to do this, but one of them kept giving me some bogus that I needed to add dependencies from my services.js as well (i.e. $ionicPopup), which might have some truth to it, but I'm super new to all of this. Any help would be greatly appreciated.

I have here my controllers.tests.js file...

    describe('Controllers', function(){
     var $rootScope, $controller, $sharedProperties, $resource;
     beforeEach(module('myApp.services', 'myApp.controllers'));
     beforeEach(inject(function($controller, $rootScope) {
        myScope = $rootScope.$new();
        ctrl = $controller('LoginController', {
          $scope: myScope,
          $state: myScope,
          LoginService: myScope,
          $cordovaTouchID: myScope,
          $ionicLoading: myScope,
          localStorageService: myScope,
          sharedProperties: myScope,
       });
describe('$scope.login()', function() {
    var $scope, controller;
    beforeEach(function() {
        $scope = {};
        $state = {};
        LoginService = {};
        localStorageService = {};
        sharedProperties = {};
        controller = $controller('LoginController', { $scope: $scope, $state: $state, LoginService: LoginService, localStorageService: localStorageService, sharedProperties: sharedProperties});
    });

it('supplies correct input', function() {
    $scope.credentials.username = "uname";
    $scope.credentials.password = "pword";
    $scope.login();
    expect(scope.credentials.password).toEqual("");
});
});
});

And here's my controllers.js

.controller('LoginController', function($scope, $state, LoginService, $cordovaTouchID, $ionicLoading, localStorageService) {
$scope.credentials = {};
$scope.show = function(){
$ionicLoading.show({
        template: "Logging In"
    });
};
$scope.login = function() {
    $scope.show();
    LoginService.initiateRequest(
    $scope.credentials.username, 
    $scope.credentials.password).then(function(){
        localStorageService.set('username', $scope.credentials.username);
        $ionicLoading.hide();
        $scope.credentials.password = "";
        $state.go('main');
    });
};
})

Aucun commentaire:

Enregistrer un commentaire