85494

ReferenceError: module is not defined in Jasmine Angular js

Question:

I am begining with Angular and with unit testing, this is my first code, and it isn't working. I searched for a solution but I dont know what I am doing wrong. If anyone can explain to me what is the mistake, I would thank you.

This is all my code.

<!DOCTYPE html> <html lang="en"> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-mocks.js"></script> <script src="jasmine.js"></script> <script src="jasmine-html.js"></script> <script src="boot.js"></script> <link rel="shortcut icon" type="image/png" href="jasmine_favicon.png"> <link rel="stylesheet" href="jasmine.css"> </head> <body ng-app = "myApp"> <div ng-controller = "MyCtrl"> {{greeting}} </div> <br><br><br> <script> <!-- CODE --> var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl', ['$scope', function($scope) { $scope.greeting = 'Hello World!'; }]); <!-- JASMINE --> describe('myApp', function () { var scope, controller; beforeEach(function () { module('myApp'); }); describe('MyCtrl', function () { beforeEach(inject(function ($rootScope, $controller) { scope = $rootScope.$new(); controller = $controller('MyCtrl', { '$scope': scope }); })); it('sets the greeting', function () { expect(scope.greeting).toBe('Hello World!'); }); }); }); describe('JavaScript addition operator', function () { it('adds two numbers together', function () { expect(1 + 2).toEqual(3); }); }); </script> </body> </html>

Thanks in advance

Answer1:

The sequence of your scripts is wrong. You should first include the scripts related to Jasmine, and then the scripts for angular and angular-mocks.

<!DOCTYPE html> <html lang="en"> <head> <!--Scripts for Jasmine--> <script src="jasmine.js"></script> <script src="jasmine-html.js"></script> <script src="boot.js"></script> <!--Script for Angular--> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> <!--Scripts for Angular-Mocks--> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-mocks.js"></script> <link rel="shortcut icon" type="image/png" href="jasmine_favicon.png"> <link rel="stylesheet" href="jasmine.css"> </head> <body ng-app = "myApp"> <div ng-controller = "MyCtrl"> {{greeting}} </div> <br><br><br> <script> <!-- CODE --> var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl', ['$scope', function($scope) { $scope.greeting = 'Hello World!'; }]); <!-- JASMINE --> describe('myApp', function () { var scope, controller; beforeEach(function () { module('myApp'); }); describe('MyCtrl', function () { beforeEach(inject(function ($rootScope, $controller) { scope = $rootScope.$new(); controller = $controller('MyCtrl', { '$scope': scope }); })); it('sets the greeting', function () { expect(scope.greeting).toBe('Hello World!'); }); }); }); describe('JavaScript addition operator', function () { it('adds two numbers together', function () { expect(1 + 2).toEqual(3); }); });

Recommend

  • angular Scroll div on button click
  • Getting webGL error in autodesk viewer
  • Docker container for google cloudML on compute engine - authenticating for mounting bucket
  • Youtube upload API and cordova / phonegap
  • How to re-render views using Angular JS
  • Sencha Touch 2.1 native (android) app not getting json from remote (it works on PC)
  • SCons libraries and sub-libraries
  • What's an elegant way of accessing parent controller's member from child controller?
  • How do I obtain a list of files that changed from svn over a date range?
  • Accessing 3rd level of JSON with Angular ng-repeat
  • RabbitMQ java client stops consuming messages
  • integrate POCO library in android ndk
  • PHP get selected value of select form
  • Slicing an SPA into several components and use AngularJS
  • Cannot page through all results using nextPageToken on YouTube search API v3
  • to implement a spinner in angular2+
  • Google OAuth: can't get refresh token with authorization code
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • How to view images from protected folder with php?
  • Adjust width of select element according to selected option's width
  • Display images in Django
  • JSON response opens as a file, but I can't access it with JavaScript
  • Django rest serializer Breaks when data exists
  • Meteor helpers not available in Angular template
  • Cannot connect to cassandra from Spark
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • RectangularRangeIndicator format like triangular using dojo
  • HTML download movie download link
  • Resize panoramic image to fixed size
  • Updating server-side rendering client-side
  • Cross-Platform Protobuf Serialization
  • MySQL WHERE-condition in procedure ignored
  • Importing jscolor library in angular 2
  • Web-crawler for facebook in python
  • Revoking OAuth Access Token Results in 404 Not Found
  • trying to dynamically update Highchart column chart but series undefined
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • java string with new operator and a literal