Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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
Node.js Angularjs单元测试错误_Node.js_Angularjs_Unit Testing_Testing - Fatal编程技术网

Node.js Angularjs单元测试错误

Node.js Angularjs单元测试错误,node.js,angularjs,unit-testing,testing,Node.js,Angularjs,Unit Testing,Testing,我不熟悉编写单元测试。我正在尝试一件简单的事情,在controller$scope的saveInfo函数中。practice应该包含一个值 我正在使用webstorm运行我的测试。我想在saveInfo函数中检查是否定义了$scope.practice test/practice.js 'use strict'; describe('myApp', function() { describe('Controller: PracticeCtrl', function () {

我不熟悉编写单元测试。我正在尝试一件简单的事情,在controller
$scope的
saveInfo
函数中。practice
应该包含一个值

我正在使用webstorm运行我的测试。我想在
saveInfo
函数中检查是否定义了$scope.practice

test/practice.js

'use strict';
describe('myApp', function() {
    describe('Controller: PracticeCtrl', function () {
        var $http, $scope, $routeParams, $location, $locale, $timeout;
      // load the controller's module
        beforeEach(function () {
            // Load the controller's module
            module('myApp');

            inject(function ($controller, $rootScope) {
                $scope = {};
            });

        });

        it('should define a practice property', function () {
            expect($scope.practice).toBeDefined();
        });

    });
});
practice.js-控制器

angular.module('myApp').controller('PracticeCtrl', function ($http, $scope, $routeParams, $location, $locale, $timeout) {

$scope.saveInfo = function () {
        $scope.practice = '52300099';
        Practices.updateStampApproval().updateInfo($scope.practice);  

 };

$scope.updateInfo = function () {
   ....
}

$scope.getInfo = function () {
   ....
}
给我一个错误:-

Expected undefined to be defined.
Error: Expected undefined to be defined.
在我的控制器中定义了$scope.practice。那么为什么它会显示错误呢


如何测试
函数
wise意味着如何编写只检查控制器中1个函数的测试,例如“saveInfo”?

您正在将
$scope
初始化为空对象,并且它不会在任何地方被操作。这就是你考试不及格的原因

你应该做什么:

  • 使用虚拟
    $scope
  • 在每个测试中调用每个控制器方法
  • 在每次测试中都要做你的断言
  • 例如:

    describe('myApp', function() {
        describe('Controller: PracticeCtrl', function () {
            var $http, $scope, $routeParams, $location, $locale, $timeout, practiceCtrl;
    
            // load the controller's module
            beforeEach(function () {
                // Load the controller's module
                module('myApp');
    
                inject(function ($controller, $rootScope) {
                    $scope = $rootScope.$new();
                    practiceCtrl = $controller("PracticeCtrl", {
                        $scope: $scope
                    });
                });
    
            });
    
            it('should define a practice property', function () {
                $scope.saveInfo();
                expect($scope.practice).toBeDefined();
            });
    
        });
    });
    

    我希望我能清楚地帮助您。

    这一点变得清楚了,但我有一个疑问……如果我在我的
    控制器中使用
    服务,它会带来一个实践列表,我是否能够在我的
    test.js中调用此服务?是的。你可以模拟/存根它来返回一些示例实践,因此你不必依赖服务器的数据。我实际上需要服务器的数据…因为这样我就不必编写模拟服务&而是使用真正的服务…如果你知道任何教程,我可以一步一步地学习…我搜索了谷歌,但还没有完美的教程。我现在没有任何教程。我说不要依赖服务器,因为如果你重新安装你的应用程序,它可能会破坏你的测试(这样你的数据库就没有数据了)