2015-06-24 83 views
1

这是我的代码,我得到以下错误,无法弄清楚。无法读取undefined属性'expectGET'

describe('Myctrl', function() { 

    var $httpBackend, scope, createController, authRequestHandler; 

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

     alert("Hello there!"); 
    beforeEach(inject(function ($injector) { 
    // Set up the mock http service responses 
alert("Hello there!"); 
    $httpBackend = $injector.get('$httpBackend'); 

    // backend definition common for all tests 
alert("Hello there!"); 
    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() { 

     //create expectation 
    $httpBackend.expectGET('http://www.w3schools.com/angular/customers.php'); 

    var controller = createController() 
    $httpBackend.flush(); 
    expect(scope.names).toBeTruthy(true); 



    }); 


}); 

我尝试了一些不同的东西,但我仍然无法弄清楚什么是错的。这似乎没有错。这种情况下的解决方案是什么?

回答

0

我重构了一下你的代码。不过,我不确定问题是否存在。我想,你也可能不包括ngMock(https://docs.angularjs.org/api/ngMock),这就是为什么你得到这样的错误

describe('Myctrl', function() { 

    var $httpBackend, $scope, createController; 

    // Set up the module 
    beforeEach(module('myApp', ['ngMock'])); 

    beforeEach(inject(['$httpBackend', '$rootScope', '$controller', function (_$httpBackend_, $rootScope, $controller) { 
    // Set up the mock http service responses 
    $httpBackend = _$httpBackend_; 

    // backend definition common for all tests 
    $httpBackend.whenGET('http://www.w3schools.com/angular/customers.php').respond(function() { 
     return [200, true]; 
    }); 

    // Get hold of a scope (i.e. the root scope) 
    $scope = $rootScope.$new(); 

    createController = function() { 
     return $controller('Myctrl', { '$scope': $scope }); 
    }; 
    }])); 


    afterEach(function() { 
    $httpBackend.verifyNoOutstandingExpectation(); 
    $httpBackend.verifyNoOutstandingRequest(); 
    }); 

    it('should fetch authentication token', function() { 
    //create expectation 
    $httpBackend.expectGET('http://www.w3schools.com/angular/customers.php'); 
    var controller = createController(); 
    $httpBackend.flush(); 
    expect($scope.names).toBeTruthy(); 
    }); 

}); 
+0

我加入了角的js和角mock.js但还是错误是一样的\ – NAND

+0

如何包括ngMock模块吗? (module('myApp')); ** with ** beforeEach(module('myApp',['ngMock'])); **会起作用。我会用它修改我的帖子 –

+0

这是行不通的,我试了一下,但仍然收到错误 – NAND

相关问题