Javascript 单元测试AngularJS服务?

Javascript 单元测试AngularJS服务?,javascript,angularjs,unit-testing,service,karma-runner,Javascript,Angularjs,Unit Testing,Service,Karma Runner,嘿,我的测试服务需要帮助。 我有这个服务:MyService.js 和此控制器: angular.module('MyControllers', []) .controller('MyCtrl2', ['$scope', 'FnEncode', function ($scope, FnEncode) { $scope.encoded1 = ""; $scope.encoded2 = ""; $scope.encoded3 = "";

嘿,我的测试服务需要帮助。

我有这个服务:MyService.js

和此控制器:

angular.module('MyControllers', [])

.controller('MyCtrl2', ['$scope', 'FnEncode', function ($scope, FnEncode) {

        $scope.encoded1 = "";
        $scope.encoded2 = "";
        $scope.encoded3 = "";

        $scope.encode1 = function() {
            $scope.encoded1 = FnEncode.encodeUUI(FnEncode.encodeFunctionalNumber($scope.numberToEncode));
        };

        $scope.encode2 = function() {
            $scope.encoded2 = FnEncode.encodeUUI(FnEncode.encodeFunctionalNumber($scope.numberToEncode)+
                FnEncode.encode2($scope.EncodeWith2));
        };

        $scope.encode3 = function() {
            $scope.encoded3 = FnEncode.encodeUUI(FnEncode.encodeFunctionalNumber($scope.numberToEncode)+
                FnEncode.encode3($scope.EncodeWith3));
        };
    }]);
'use strict';

describe('My services', function() {

    beforeEach(module('myApp'));

    beforeEach(module('MyServices'));

    describe('MyCtrl2', function() {

       var scope, ctrl;
        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new();
            ctrl = $controller('MyCtrl2', {$scope: scope});
        }));

        it('should output: ', function(scope) {
            expect(scope.encoded1.toBe("00050221F3"));     
        });
    });
});
当我在文本字段中输入123时,该服务基本上是这样做的,它输出我00050221F3,其中前00是encodeUUI。 我确实测试了类似的内容,但它显示无法读取属性encoded1:

angular.module('MyControllers', [])

.controller('MyCtrl2', ['$scope', 'FnEncode', function ($scope, FnEncode) {

        $scope.encoded1 = "";
        $scope.encoded2 = "";
        $scope.encoded3 = "";

        $scope.encode1 = function() {
            $scope.encoded1 = FnEncode.encodeUUI(FnEncode.encodeFunctionalNumber($scope.numberToEncode));
        };

        $scope.encode2 = function() {
            $scope.encoded2 = FnEncode.encodeUUI(FnEncode.encodeFunctionalNumber($scope.numberToEncode)+
                FnEncode.encode2($scope.EncodeWith2));
        };

        $scope.encode3 = function() {
            $scope.encoded3 = FnEncode.encodeUUI(FnEncode.encodeFunctionalNumber($scope.numberToEncode)+
                FnEncode.encode3($scope.EncodeWith3));
        };
    }]);
'use strict';

describe('My services', function() {

    beforeEach(module('myApp'));

    beforeEach(module('MyServices'));

    describe('MyCtrl2', function() {

       var scope, ctrl;
        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new();
            ctrl = $controller('MyCtrl2', {$scope: scope});
        }));

        it('should output: ', function(scope) {
            expect(scope.encoded1.toBe("00050221F3"));     
        });
    });
});

我希望有人能告诉我哪里做错了?

如果调用函数不是为了对值进行编码,请尝试:

it('should output: ', function() {
     scope.numberToEncode = "123";
     scope.encode1();
     expect(scope.encoded1.toBe("00050221F3"));     
});
然而,这不是我们应该如何对服务进行单元测试。为了对服务进行单元测试,我们分别测试服务的每个功能

这种验证
scope.encode1
功能的测试也不正确。我们应该模拟
FnEncode
,并验证
FnEncode
的函数是按预期顺序调用的

要测试您的服务,您应该执行以下操作:

'use strict';

describe('My services', function() {

    beforeEach(module('myApp'));

    beforeEach(module('MyServices'));

    describe('MyCtrl2', function() {

        var encode;
        beforeEach(inject(function(FnEncode) {
            encode = FnEncode; //inject your service and store it in a variable
        }));

        //Write test for each of the service's functions

        it('encode Functional Number: ', function() {
            var encodedValue = encode.encodeFunctionalNumber("123");
            expect(encodedValue).toBe("00050221F3");  //replace 00050221F3 with your expected value
        });

        it('encode UUI: ', function() {
            var encodedValue = encode.encodeUUI("123");
            expect(encodedValue).toBe("00050221F3");  //replace 00050221F3 with your expected value
        });
    });
});

为什么要使用$scope.encoded1和$scope.encode1?您可以使用$scope.encode1,让函数返回encodeUUI()的结果,而不是将其分配给encoded1。谢谢!!!!它起作用了。我刚刚更改了中的最后一行:expect(encode.encodeUUI(encodedValue)).toBe(“00050221F3”);