【问题标题】:Unit Testing a AngularJS service?单元测试 AngularJS 服务?
【发布时间】:2014-04-14 11:22:59
【问题描述】:

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

我有这个服务: 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));
        };
    }]);

当我在文本字段中输入 123 时,服务基本上是在做的,它输出我 00050221F3,其中前 00 是 encodeUUI。 我确实测试过这样的东西,但它说无法读取属性编码1:

'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"));     
        });
    });
});

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

【问题讨论】:

  • 你为什么使用 $scope.encoded1 和 $scope.encode1?您可以只使用 $scope.encode1 并让函数返回 encodeUUI() 的结果,而不是将其分配给 encoded1。

标签: javascript angularjs unit-testing service karma-runner


【解决方案1】:

你没有调用你的函数来编码值,试试:

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
        });
    });
});

【讨论】:

  • 谢谢!!!!它的工作。我刚刚更改了最后一行:expect(encode.encodeUUI(encodedValue)).toBe("00050221F3");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 2012-12-23
  • 2012-05-20
相关资源
最近更新 更多