Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 测试时未执行angular js controlles的函数_Angularjs_Jasmine_Karma Runner - Fatal编程技术网

Angularjs 测试时未执行angular js controlles的函数

Angularjs 测试时未执行angular js controlles的函数,angularjs,jasmine,karma-runner,Angularjs,Jasmine,Karma Runner,我正在用Jasmine和Karma测试我的angularjs应用程序 我的测试如下所示: describe('Login test', function() { // Mock our module in our tests beforeEach(module('Project')); var ctrl, scope; // inject the $controller and $rootScope services // in the beforeEach block

我正在用Jasmine和Karma测试我的angularjs应用程序

我的测试如下所示:

describe('Login test', function() {
  // Mock our module in our tests
  beforeEach(module('Project'));

  var ctrl, scope;
  // inject the $controller and $rootScope services
  // in the beforeEach block
  beforeEach(inject(function($controller, $rootScope) {
    // Create a new scope that's a child of the $rootScope
    scope = $rootScope.$new();

    // Create the controller
    ctrl = $controller('loginController', {
        $scope: scope
    });
  }));

  it('should get login success',function() {
    scope.loginClick('user', 'pass');
  });
});
app.controller('loginController', ['$scope', '$http', '$route', function ($scope, $http, $route) {
  console.log('controller call'); // yes it works
  ...
  $scope.loginClick = function (username, password) {
    console.log('controller call'); // yes it works

    handler.reqPOST('/login', userData, function (result) {
      console.log('login request'); // no output is sent to the console
    });
 };
}]);
我有一个带有loginClick函数的登录控制器,在这个函数中我有另一个函数,它发出POST请求。问题是内部函数从未执行过,我尝试console.log()查看是否调用了该函数,但没有成功。 我的函数如下所示:

describe('Login test', function() {
  // Mock our module in our tests
  beforeEach(module('Project'));

  var ctrl, scope;
  // inject the $controller and $rootScope services
  // in the beforeEach block
  beforeEach(inject(function($controller, $rootScope) {
    // Create a new scope that's a child of the $rootScope
    scope = $rootScope.$new();

    // Create the controller
    ctrl = $controller('loginController', {
        $scope: scope
    });
  }));

  it('should get login success',function() {
    scope.loginClick('user', 'pass');
  });
});
app.controller('loginController', ['$scope', '$http', '$route', function ($scope, $http, $route) {
  console.log('controller call'); // yes it works
  ...
  $scope.loginClick = function (username, password) {
    console.log('controller call'); // yes it works

    handler.reqPOST('/login', userData, function (result) {
      console.log('login request'); // no output is sent to the console
    });
 };
}]);

handler对象在启动时包含在karma配置文件中。

首先,除非您有很好的理由,
$http
是angularJS中调用后端的方法,它还使其更易于测试

在任何情况下,您都应该模拟post调用,在单元测试中,您不希望依赖后端

在您的情况下,可以使用spy():


如果reqPOST发出http请求,您需要在ngMock中使用$httpBackend服务设置模拟http后端。
spyOn
正在创建
handler
的模拟,因此在本例中模拟post调用。expect(handler.reqPOST).toHaveBeenCalled();-此测试通过,但问题是实际的POST请求永远不会发送模拟的全部要点是用可预测的伪请求替换后端请求,请参阅