Wie unit-Tests (mit Jasmin) eine Funktion in einer Steuerung, die fordert, eine Fabrik-service, die eine Zusage zurückgibt

In der unten SampleController, wie kann ich das Gerät testen, dass postAttributes Funktionsaufrufe sampleService.updateMethod. Ich habe Probleme seit der updateMethod Renditen Versprechen.

    angular.module('sampleModule')
       .controller('SampleController', SampleController);

    SampleController.$inject =['sampleService'];

    function SampleController(sampleService){

    this.postAttributes = function() {    
        sampleService.updateMethod(number,attributes)
            .then(function(response){
                //do something on successful update
            },function(response){
                //do something on unsuccessful update
            });
        }; 

    }

Hier ist die Fabrik-service, die ich habe:

    angular.module('sampleModule')
        .factory('sampleService', sampleService);

    sampleService.$inject = ['$http'];

    function sampleService($http) {
        return {
            getMethod: function(acctNumber){
                return $http({
                    method: 'GET',
                    url: //api endpoint
                });
            },
            updateMethod: function(number, attributes){
                return $http({
                    method: 'PUT',
                    url: //api endpoint,
                    data: //payload
                });
            }
        };
    }

Möchte ich verspotte die Fabrik-service in den controller-spec als Injektion die tatsächliche service direkt in $controller, da die meisten unit-Test-Richtlinien festlegen, um zu testen, eine Einheit unter isolation.

Probe controller spec:

    describe('SampleController Test', function(){
        var $controller;
        var service;

        beforeEach(angular.mock.module('sampleModule')); 

        beforeEach(angular.mock.inject(function(_$controller_){
            $controller = _$controller_;
        }));

        it('Testing $scope variable', function(){
            var sampleController = $controller('SampleController', { 
                sampleService: service, //mocked factory service 
            });

            sampleController.postAttributes(); //calling the function first
            //here I would like to make an assertion to check if
            //sampleService.updateMethod has been called with certain parameters                        
            //how do I do that??
        });

    });
InformationsquelleAutor Pramodh | 2016-04-06
Schreibe einen Kommentar