1

I dont know if its a valid question but as long as i inject ngRoute into my app my $scope is undefined.

the app without ng-Route looks like this

angular.module('DummyApp', [])
.controller('DummyCtrl', ['$scope',  function ($scope) {
    $scope.name = "World";
}]);

the test case like this

describe( 'Dummy Controller', function(){
    var scope, http, DummyCtrl;

    beforeEach(module('DummyApp'));
    beforeEach(inject(function($controller, $rootScope) {

        scope = $rootScope.$new();
        DummyCtrl = $controller('DummyCtrl', {
            $scope: scope
        });
    }));

    it('should contain the word world', function(){
        expect(scope.name).toEqual("World");
    });

});

passing without any error

but if i inject the ngRoute to my app

angular.module('DummyApp', ['ngRoute'])
.controller('DummyCtrl', ['$scope',  function ($scope) {
    $scope.name = "World";
}]);

TypeError: 'undefined' is not an object (evaluating 'scope.name')


I also tried to configure the $routeProvider see below but i just don't know how to write my test if the ngRoute is injected

angular.module('DummyApp', ['ngRoute']).app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
    when('/smth', {
        templateUrl: 'smth.html',
        controller: 'testController'
    });
}])
.controller('DummyCtrl', ['$scope',  function ($scope) {
    $scope.name = "World";
}]);
anuloo
  • 15
  • 10
  • Did you include all required js files in your test environment or karma config? i.e. angular-route.js – sdfacre Jan 24 '16 at 23:33
  • I have spent days on this nothing makes sense as long as i include the ngRoute my $scope is lost, – anuloo Jan 25 '16 at 03:35
  • in your karma.config, looks like your 'node_modules/angular-route' is only pointing to the directory. – sdfacre Jan 25 '16 at 03:49
  • files: [ 'node_modules/angular/angular.js', 'node_modules/angular-route.js', 'node_modules/angular-mocks/angular-mocks.js', 'app/public/**/*.js', 'test/**/*.js' ] – anuloo Jan 25 '16 at 03:51
  • no i checked it and it has the .js – anuloo Jan 25 '16 at 03:52
  • do you mind create a plunker so I can help you better? – sdfacre Jan 25 '16 at 03:58
  • I have tried till this time but i cant recreate it in plunket , there is no karma -jasmine package, and i have no idea how to use this site – anuloo Jan 25 '16 at 04:12
  • also there is no angular-mocks package – anuloo Jan 25 '16 at 04:16
  • you have an additional 'app' on this line angular.module('DummyApp', ['ngRoute']).app.config. This must be a copy and paste issue. it should be angular.module('DummyApp', ['ngRoute']).config – sdfacre Jan 25 '16 at 04:24
  • i manage the plunker [link](http://plnkr.co/edit/eJZYNpguaKGln4UIaZRk?p=preview) – anuloo Jan 25 '16 at 04:51
  • my last comment should have solved your problem... – sdfacre Jan 25 '16 at 04:53
  • why do you need to inject $routeProvider in your test? – sdfacre Jan 25 '16 at 05:44
  • if you want to test your route config, see http://stackoverflow.com/questions/17963717/can-we-write-unit-test-for-angularjs-routeprovider – sdfacre Jan 25 '16 at 05:49
  • thanks but i manage to make it work, it was the angular and angular-route was a different version . – anuloo Jan 25 '16 at 07:25
  • no problem. I guess the first question I should ask should be: - what version are you using. hehe – sdfacre Jan 25 '16 at 08:30

0 Answers0