3

I'm to setup standalone jasmine testing. I believe I have everything setup correctly. When I run my first test:

describe('Logon Controller', function() {
    var controller, $scope;

    beforeEach(module("app"));

    //beforeEach(inject(function ($rootScope, $controller) {
    //    scope = $rootScope.$new();
    //    contoller = $controller('logonCtrl', {
    //        $scope: scope
    //    });
    //}));

    beforeEach(inject(function ($rootScope) { 
        scope = $rootScope.$new();
    }));

    //it('should say hello', function ($controller, $rootScope) {
    //    //var scope = $rootScope.$new();
    //    var controller = $controller('logonCtrl', { $scope: $scope });
    //    //expect(angular.isFunction(scope.get)).toEqual("Hello There :)");
    //    expect(scope.Hello()).toEqual("Hello There :)");
    //    expect($scope.Hello).toBeDefined();
    //});

    it('should say hello', inject(function ($controller, $rootScope) {
        //var scope = $rootScope.$new();
        var controller = $controller('logonCtrl', {$scope: $scope});
        //expect(angular.isFunction(scope.get)).toEqual("Hello There :)");
        //expect(scope.Hello()).toEqual("Hello There :)");
        expect($scope.Hello).toBeDefined();
    }));

});

I get this error message: Error: [$injector:modulerr] http://errors.angularjs.org/1.3.11/$injector/modulerr?p0=app.............

Here is my app module config.

var app = angular.module('app', ['ui.router','ngMaterial']);

Here is my controller:

app.controller('logonCtrl', ['$scope', '$state', '$interval', function ($scope, $state, $interval) {
  $scope.hello = function() {};
});
]);

I am using the standalone version of Jasmine 2.2.0. I have no clue what the issue could be, everything I have done is pretty basic stuff. Any help would be very much appreciated.

UPDATE: here is what my SpecRunner.html looks like:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Jasmine Spec Runner v2.2.0</title>

  <link rel="shortcut icon" type="image/png" href="JasmineUnitTest/lib/jasmine-2.2.0/jasmine_favicon.png">
  <link rel="stylesheet" href="JasmineUnitTest/lib/jasmine-2.2.0/jasmine.css">

  <script type="text/javascript" src="JasmineUnitTest/lib/jasmine-2.2.0/jasmine.js"></script>
  <script type="text/javascript" src="JasmineUnitTest/lib/jasmine-2.2.0/jasmine-html.js"></script>
  <script type="text/javascript" src="JasmineUnitTest/lib/jasmine-2.2.0/boot.js"></script>
  <script type="text/javascript" src="JasmineUnitTest/lib/jasmine-2.2.0/angular.min.js"></script>
  <script type="text/javascript" src="JasmineUnitTest/lib/jasmine-2.2.0/angular-mocks.js"></script>

  <!-- include source files here... -->
  <script type="text/javascript" src="js/controllers/logonCtrl.js"></script>      

  <!-- include spec files here... -->
  <script type="text/javascript" src="JasmineUnitTest/specs/logonController-spec.js"></script>
    
</head>

<body>
</body>
</html>

8
  • The part beforeEach(inject(function ($rootScope) { scope = $rootScope.$new(); })); should have $scope not scope. Is it a typo? Commented Apr 2, 2015 at 14:39
  • It's not a typo, I was under the assumption that a you could create a variable that holds a new instance of $rootScope, I didn't the name of the variable would make a difference. I'll give that a shot and I'll you know it that helps. Thanks! Commented Apr 2, 2015 at 14:47
  • I'm still getting the same error :( and it was a typo, I noticed that I did declared that variable as "$scope" lol. Commented Apr 2, 2015 at 14:49
  • Click the debug button on the jasmine browser window. once a new window opens do f12 and check the source tab. Are all the script files loaded correctly? Commented Apr 2, 2015 at 15:05
  • I would define 'controller' inside the beforeEach function and instantiate same there (just thinking you'd want to add more tests in the future?), that way your it statement does not have to instantiate the ctrl each time. Also just noticed controller should be a function in your test. Commented Apr 2, 2015 at 15:16

1 Answer 1

2

It looks like the $injector fails to find some of your dependencies. Are you sure the script containing your controller is properly loaded ?

Sign up to request clarification or add additional context in comments.

4 Comments

That's what I'm a bit concerned about. how can you tell if a controller is properly loaded. When deploying the app don't have any issues, at none that I have come across.
It's probably the js file that is not properly loaded in the tests. Is your controller in a separate file ?
Yes my controller is in a separate file. In the console for the browser dev tools (firebug) it cannot find my controller js file. I have no idea why though.
Check the jasmine documentation about how to properly load the controller file. This will fix your problem. :)

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.