TypeError Unit Test Jasmine

snurby77

I need some help, I'm making some progress towards actually reading through a whole file, however, I've hit a decently sized roadblock (for me it's decent, I'm new to it). I'm trying to mock up most of a controller and I run into a problem when I get about half way through reading the file and I have no idea how to overcome it. I'll include as much code as I can below.

LocationController.js

angular
.module('TDE')
.controller('LocationController', ['$rootScope', '$scope', '$location', '$window', '$document', 'LocationService', 'HeaderFooterService', 'SearchService', 'TranslationService', 'MTDE_CONFIG', 'LocationPartnerAssignmentService', 'ExperimentService', function ($rootScope, $scope, $location, $window, $document, $LocationService, $HeaderFooterService, $SearchService, $TranslationService, $MTDE_CONFIG, $LocationPartnerAssignmentService, $ExperimentService) {
//passes over this code

$scope.init = function () {
    // load dropdown for crop list

    // load dropdown for business partner list

    //load dropdown for experimenttype

    //load dropdown for IrrigationType
    //load dropdown for previousCrop

    var locationId = $SearchService.GetLocationId();
    $scope.LocationId=locationId;
    var experimentId = $SearchService.GetExperimentId();
    var experimentPartnerAssignmentId = $SearchService.GetExperimentPartnerAssignmentId();

    //load nitrogen value

    $ExperimentService.GetCrop(onSuccessCropCode,cropId);

    // get location detail from database
    if (locationId === "00000000-0000-0000-0000-000000000000") {
        // Load the Unassigned Location here
        $scope.IsAssinged = false;
    }
    else {
        $LocationService.Detail(onSuccessDetail, locationId);
    }

    // get growing year list
    $scope.GrowingYearList = $LocationService.GrowingYearList();
}

$scope.init(); //I NEED TO BE MOCKED SOMEHOW

Helper.js

ddescribe("Phases of Testing: The Journey", function () {

describe("Phase I: Test that Jasmine runs", function () {
    it("should test Jasmine is up", function () {
        expect(true).toBe(true);
    });
});

describe("Phase II: Try something", function () {
    var DBMock, SyncMock, baseMock, configMock, TransMock, loginMock, locMock, headerMock,
            searchMock, LPAMock, expMock, mockScope, $location, $scope, ctrl, initMock;
    beforeEach(function () {
        angular.mock.module('TDE');
        inject(function (_$location_, _$rootScope_, _$controller_) {
            $location = _$location_;
            $scope = _$rootScope_.$new();
            ctrl = _$controller_('LocationController', {
                '$scope': $scope
            });
        });
    });
    it("should be able to grab something", function () {
        expect(true).toBe(true);
        expect($scope).toBeDefined();
        expect($scope.PlantingDateNull()).toBeTruthy();
    });
  });
});

EDIT: Now the problem comes down to mocking the $scope.init() function call after it is initially defined. I can't find a good source for help on this anywhere.

Krzysztof Safjanowski

Application:

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function ($scope, environment) {
    $scope.testValue = 'test environment';

    $scope.init = function() {
        $scope.testValue = 'development environment';
    };

    environment.development && $scope.init();
});

myApp.factory('environment', function() {
    return {
        development: true
    }
});

The most important line is environment.development && $scope.init();

environment.development:

  • if it is evaluated to TRUE$scope.inint() will be called
  • evaluated to FALSE – will prevent for calling $scope.inint()

To prove it I’m going to stub this servis with two values

Test:

beforeEach(function () {
    module('myApp');
});

beforeEach(function () {
    module(function ($provide) {
        $provide.value('environment', mockEnvironment);
    });
});

describe('MyCtrl', function () {
    beforeEach(inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        controllerInstantiate = $controller;
    }));

    it('prevents call .init() on test environment', function () {
        mockEnvironment.development = false;
        createController();
        expect(scope.testValue).toBe('test environment');
    });

    it('calls init on development environment', function () {
        mockEnvironment.development = true;
        createController();
        expect(scope.testValue).toBe('development environment');
    });

    function createController() {
        return controllerInstantiate('MyCtrl', {
            '$scope': scope
        });
    }
});

http://jsfiddle.net/krzysztof_safjanowski/2LUp3/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeError: $scope is undefined in Angular controller unit test with Jasmine

From Dev

Jasmine Unit Test with postMessage and addEventListener

From Dev

$httpBackend in AngularJs Jasmine unit test

From Dev

AngularJS controller unit test with Jasmine

From Dev

Unit Test for Login with Jasmine and Karma

From Dev

Jasmine unit test module is not defined

From Dev

"TypeError: createController is not a function in" jasmine test

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

Why is my jasmine unit test not waiting for 'done'?

From Dev

Jasmine Unit Test Angular-Cookie

From Dev

Jasmine unit test case for $routeChangeStart in AngularJS

From Dev

Jasmine unit test skipped by my Karma configuration

From Dev

How to unit test a chained method using Jasmine

From Dev

angularjs unit test karma with jasmine mock dilemma

From Dev

Angular Unit Test Jasmine Spy error

From Dev

Jasmine unit test asynchronous controller method

From Dev

How to write unit test to Restangular with Jasmine?

From Dev

Writing unit test for AngularJS directive with Jasmine?

From Dev

Jasmine Unit Test, override only property in service

From Dev

Jasmine Unit Test, override only property in service

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

Jasmine unit test asynchronous controller method

From Dev

Angular Unit Test Jasmine Spy error

From Dev

Why is my jasmine unit test not waiting for 'done'?

From Dev

angular karma jasmine unit test for a controller

From Dev

'undefined is not an object' in karma/jasmine unit test

From Dev

Karma Jasmine unit test for Kendo template

From Dev

AngularJS Jasmine test: TypeError: 'undefined' is not an object

From Dev

AngularJS TypeError: 'undefined' is not an object' Jasmine test

Related Related

  1. 1

    TypeError: $scope is undefined in Angular controller unit test with Jasmine

  2. 2

    Jasmine Unit Test with postMessage and addEventListener

  3. 3

    $httpBackend in AngularJs Jasmine unit test

  4. 4

    AngularJS controller unit test with Jasmine

  5. 5

    Unit Test for Login with Jasmine and Karma

  6. 6

    Jasmine unit test module is not defined

  7. 7

    "TypeError: createController is not a function in" jasmine test

  8. 8

    Unit test service that returns promise Angularjs Jasmine

  9. 9

    Why is my jasmine unit test not waiting for 'done'?

  10. 10

    Jasmine Unit Test Angular-Cookie

  11. 11

    Jasmine unit test case for $routeChangeStart in AngularJS

  12. 12

    Jasmine unit test skipped by my Karma configuration

  13. 13

    How to unit test a chained method using Jasmine

  14. 14

    angularjs unit test karma with jasmine mock dilemma

  15. 15

    Angular Unit Test Jasmine Spy error

  16. 16

    Jasmine unit test asynchronous controller method

  17. 17

    How to write unit test to Restangular with Jasmine?

  18. 18

    Writing unit test for AngularJS directive with Jasmine?

  19. 19

    Jasmine Unit Test, override only property in service

  20. 20

    Jasmine Unit Test, override only property in service

  21. 21

    Unit test service that returns promise Angularjs Jasmine

  22. 22

    Jasmine unit test asynchronous controller method

  23. 23

    Angular Unit Test Jasmine Spy error

  24. 24

    Why is my jasmine unit test not waiting for 'done'?

  25. 25

    angular karma jasmine unit test for a controller

  26. 26

    'undefined is not an object' in karma/jasmine unit test

  27. 27

    Karma Jasmine unit test for Kendo template

  28. 28

    AngularJS Jasmine test: TypeError: 'undefined' is not an object

  29. 29

    AngularJS TypeError: 'undefined' is not an object' Jasmine test

HotTag

Archive