NAND
NAND

Reputation: 130

I can't figure out the solution for the following error

My code of test_Spec.js

describe('Myctrl', function() {

   var $httpBackend, scope, createController, authRequestHandler;

   // Set up the module  
   beforeEach(module('myApp'));


    beforeEach(inject(function($injector) {
     // Set up the mock http service responses

     $httpBackend = $injector.get('$httpBackend');

     // backend definition common for all tests

     authRequestHandler = $httpBackend.when('GET', 'http://www.w3schools.com/angular/customers.php')
                            .respond(true);

     // Get hold of a scope (i.e. the root scope)

     $rootScope = $injector.get('$rootScope');

     // The $controller service is used to create instances of controllers
     var $controller = $injector.get('$controller');


     createController = function() {

       return $controller('Myctrl', {'$scope' : scope});

     };

   })

);


   afterEach(function() {

     $httpBackend.verifyNoOutstandingExpectation();

     $httpBackend.verifyNoOutstandingRequest();


   });

   it('should fetch authentication token', function() {
     $httpBackend.expectGET('http://www.w3schools.com/angular/customers.php');


     var controller = createController();

     expect(scope.names).toBe(true); 

     $httpBackend.flush();

   });


});

This is my test.js service call

var myApp = angular.module('myApp', []);
  app.controller('Myctrl', function($Scope, $http) {
        $http.get("http://www.w3schools.com/angular/customers.php")
       .success(function(response) {$Scope.names = response.records;});

 });

and these are the errors that I'm getting:

Screenshot of Error

Here is a summary of the errors:

Error: [$injector:unpr] Unknown provider: $rootScopeProvider <- $rootScope <- $httpBackend http://errors.angularjs.org/1.2.0/$injector/unpr?p0=%24rootScopeProvider%20%3C-%20%24rootScope%20%3C-%20%24httpBackend

TypeError: Cannot read property 'expectGET' of undefined

TypeError: Cannot read property 'verifyNoOutstandingExpectation' of undefined

What is the solution for the errors? I have tried several things but I can't figure out the exact solution. Please assist on this.

Upvotes: 0

Views: 521

Answers (2)

Jagrut
Jagrut

Reputation: 922

Well, I know it is too late to answer but in case anyone else stumbles upon similar error, you can try the following:

You never defined the scope variable. You just declared it. So it actually should be:

scope = $injector.get('$rootScope').$new();

Also note the $new(). For every new test case, (as a best practice) you should get a new scope.

Upvotes: 1

Tomek Sułkowski
Tomek Sułkowski

Reputation: 7201

You have a typo in your dependencies that you want to inject into your controller.

Change $Scope to $scope ;)

As for the error in your test, try injecting the services this way:

beforeEach(inject(function (_$httpBackend_, _$rootScope_, _$controller_) {
   …

Upvotes: 1

Related Questions