单元测试AngularJS服务

单元测试AngularJS服务

本文介绍了单元测试AngularJS服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有这样的服务: MyService.js

I have this service: 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都设有codeUUI。
我测试了这样的事情,但它说无法读取性能EN codeD1:

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

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

推荐答案

您不是在呼唤你的功能,EN code值,请尝试:

You're not calling your function to encode the value, try:

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

不过,这不是我们应该如何进行单元测试服务。单元测试的服务,我们分别测试服务的各项功能。

However, this is not how we should unit test the service. To unit test the service, we test each function of the service separately.

这样的测试,以验证功能的 scope.en code1 也是不正确的。我们应该嘲笑 FnEn code 并验证 FnEn code 的函数的调用预计订单。

This kind of test to verify the function of the scope.encode1 is also not correct. We should mock the FnEncode and verify that FnEncode's functions were called with expected order.

要测试你的服务,你应该做这样的事情:

To test your service, you should do something like this:

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

这篇关于单元测试AngularJS服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:39