Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.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
Javascript AngularJS表单单元测试问题_Javascript_Angularjs_Unit Testing_Mocha.js_Chai - Fatal编程技术网

Javascript AngularJS表单单元测试问题

Javascript AngularJS表单单元测试问题,javascript,angularjs,unit-testing,mocha.js,chai,Javascript,Angularjs,Unit Testing,Mocha.js,Chai,我正试图为我的表单处理控制器设置一个单元测试,但似乎在正确设置时遇到了问题 以下是控制器处理表单的代码: app.controller("FormController", ['$scope', '$http', '$window', function($scope, $http, $window) { $scope.message = ""; $scope.processForm = function() { $scope.message = "Processing

我正试图为我的表单处理控制器设置一个单元测试,但似乎在正确设置时遇到了问题

以下是控制器处理表单的代码:

app.controller("FormController", ['$scope', '$http', '$window',  function($scope, $http, $window) {
    $scope.message = "";

    $scope.processForm = function() {
     $scope.message = "Processing form";
     $scope.messageStyle = {
     "color": "green"
     };

     $http({
        method: 'POST',
        url: '/register',
        data:{
          "username": $scope.user.username,
          "email": $scope.user.email,
          "password": $scope.user.password,
          "confirm": $scope.user.confirm
        }
      }).success(function(data) {
        $scope.message = data.msg;

        if (data.success) {
          $window.location.href = "/confirm";
        } else {
          $scope.messageStyle = {
            "color": "red"
          };
        }

      });

    };
   }]);
下面是我使用Mocha和Chai的单元测试代码:

describe("FormController", function(){

  var scope;
  var ctrl;
  var httpBackend;
  var http;

   beforeEach(module("home"));

   beforeEach(inject(function($rootScope, $controller, $httpBackend, $http) {

     scope = $rootScope.$new();
     http = $http;
     ctrl = $controller("FormController", {$scope : scope, $http : http});
     httpBackend = $httpBackend;
   }));

  describe("when calling the processForm function", function(){

  beforeEach(function() {
       scope.processForm();

    });

    it("Should contain a message", function(){
      expect(scope.message).to.equal("Processing form");
    });

   });

});
运行测试时,返回以下错误:
TypeError:“undefined”不是对象(正在计算“$scope.user.username”)


我应该如何解决此问题?

请在每次访问之前尝试以下代码

 beforeEach(function() {
   elm = angular.element(
       '<form ng-controller="FormController">' +
    '<input ng-model="user.username">' +
         '<input ng-model="user.email">' +
         '<input mg-model="user.password">' +
         '<input mg-model="user.confirm">' +
        '</form>');
     scope = $rootScope;
     $compile(elm)(scope);
     scope.$digest();
        scope.processForm();

 });
beforeach(函数(){
elm=角元素(
'' +
'' +
'' +
'' +
'' +
'');
scope=$rootScope;
$编译(elm)(范围);
范围。$digest();
scope.processForm();
});

希望这能帮助您

为什么要对帖子数据进行字符串化并添加内容类型标题?Angular的
$http
为您完成了所有这些。此外,在调用
processForm
@Phil之前,您显然需要在您的作用域上创建一个
user
对象。我已经更新了我的代码,以反映POST数据更改。我的
用户
来自ng车型。因此,我在html表单中有类似于
ng model=“user.username”
的内容。控制器测试不包括任何模板,因此在调用
processForm()
之前,您必须手动设置模型数据。在您的测试中,在调用
scope.processForm()
之前,添加
scope.user={username:'foo',email:'foo@example.com', ...}
。当然,您还必须将预期的HTTP调用添加到
httpBackend