18

हमारे पास कोणीय नियंत्रक में कुछ विधियां हैं, जो स्कोप चर पर नहीं हैं।हम गैर-स्कोप कोणीय नियंत्रक विधियों का परीक्षण कैसे कर सकते हैं?

क्या कोई जानता है, हम जैस्मीन परीक्षणों के अंदर उन तरीकों को कैसे निष्पादित या कॉल कर सकते हैं?

यहां मुख्य कोड है।

var testController = TestModule.controller('testController', function($scope, testService) 
{ 

function handleSuccessOfAPI(data) { 
    if (angular.isObject(data)) 
    { 
     $scope.testData = data; 
    } 
} 

function handleFailureOfAPI(status) { 
    console.log("handleFailureOfAPIexecuted :: status :: "+status); 
} 

// this is controller initialize function. 
function init() { 
    $scope.testData = null; 

    // partial URL 
    $scope.strPartialTestURL = "partials/testView.html; 

    // send test http request 
    testService.getTestDataFromServer('testURI', handleSuccessOfAPI, handleFailureOfAPI); 
} 

init(); 
} 

अब मेरी चमेली परीक्षण में, हम "handleSuccessOfAPI" और "handleFailureOfAPI" विधि से गुजर रहे हैं, लेकिन इन अपरिभाषित है।

यहां जैस्मीन परीक्षण कोड है।

describe('Unit Test :: Test Controller', function() { 
var scope; 
var testController; 

var httpBackend; 
var testService; 


beforeEach(function() { 
    module('test-angular-angular'); 

    inject(function($httpBackend, _testService_, $controller, $rootScope) { 

     httpBackend = $httpBackend; 
     testService= _testService_; 

     scope = $rootScope.$new(); 
     testController= $controller('testController', { $scope: scope, testService: testService}); 
      }); 
}); 

afterEach(function() { 
     httpBackend.verifyNoOutstandingExpectation(); 
     httpBackend.verifyNoOutstandingRequest(); 
    }); 

it('Test controller data', function(){ 
    var URL = 'test server url'; 

    // set up some data for the http call to return and test later. 
    var returnData = { excited: true }; 

    // create expectation 
    httpBackend.expectGET(URL).respond(200, returnData); 

    // make the call. 
    testService.getTestDataFromServer(URL , handleSuccessOfAPI, handleFailureOfAPI); 

    $scope.$apply(function() { 
     $scope.runTest(); 
    }); 

    // flush the backend to "execute" the request to do the expectedGET assertion. 
    httpBackend.flush(); 

    // check the result. 
    // (after Angular 1.2.5: be sure to use `toEqual` and not `toBe` 
    // as the object will be a copy and not the same instance.) 
    expect(scope.testData).not.toBe(null); 
}); 
}); 

उत्तर

8

है के रूप में आप उन कार्यों के लिए उपयोग नहीं होगा। जब आप एक नामित जे एस समारोह को परिभाषित यह ही है जैसे कि आप कहने के लिए

var handleSuccessOfAPI = function(){}; 

जो मामले में यह देखने के लिए कि वर केवल ब्लॉक के भीतर दायरे में है बहुत स्पष्ट होगा थे और वहाँ कोई बाहरी संदर्भ है रैपिंग नियंत्रक से इसे करने के लिए।

कोई भी फ़ंक्शन जिसे विघटित रूप से (और इसलिए परीक्षण किया जा सकता है) नियंत्रक के $ दायरे पर उपलब्ध होगा।

+2

सही। इसके अलावा, हमें निजी कार्यों का परीक्षण नहीं करना चाहिए। आम तौर पर, 'init() 'इकाई को नकली डेटा के साथ परीक्षण किया जाता है जो सुनिश्चित करेगा कि' हैंडलअपऑफएपीआईआई 'या' हैंडलफेलरऑफैपी 'लागू किया गया है। मॉक ऑब्जेक्ट्स/डेटा निष्पादन के पथ को ड्राइव करना चाहिए। – dmahapatro

+0

मैं फ़ंक्शन/परिवर्तनीय सार्वजनिक (जावा की तरह कुछ की तुलना करते समय) बनाने के रूप में $ स्कोप देखता हूं। मैं जावा में यूनिट परीक्षण से अधिक परिचित नहीं हूं, लेकिन क्या आप "निजी कार्यों का परीक्षण नहीं कर रहे हैं" जो आप जावा में सामान्य रूप से करते हैं? – Chris

+0

क्रिस, मेरे पास सामान्य रूप से सीमित लेखन लेखन परीक्षण भी हैं, कुछ जुनीट परीक्षण लिखे हैं लेकिन मैं किसी भी तरह से परीक्षण या किसी भी विशेषज्ञ में विशेषज्ञ नहीं हूं। मैं कल्पना करता हूं क्योंकि निजी कार्यों का खुलासा नहीं होता है जब आप किसी ऑब्जेक्ट के इंस्टेंस को तुरंत चालू करते हैं, तब भी आप एक ही बाधा के तहत हैं कि आप केवल उन चीजों का परीक्षण कर सकते हैं जो सार्वजनिक रूप से सुलभ हैं। मुझे लगता है कि आखिरकार यह समझ में आता है क्योंकि जब आप एक परीक्षा लिखते हैं तो आप ब्लैक बॉक्स फैशन के प्रकार में एक कार्य पूरा करने वाले कोड के एक हिस्से के साथ वास्तव में चिंतित हैं, तो अगर मैं एक्स की आपूर्ति करता हूं तो मैं वाई की अपेक्षा करता हूं, लेकिन कार्यान्वयन विवरण नहीं हैं एक चिंता। – shaunhusain

9

मुझे पता है कि यह एक पुराना मामला है, लेकिन यहां वह समाधान है जिसका मैं उपयोग कर रहा हूं।

अपने नियंत्रक

.controller('newController',['$scope',function($scope){ 
    var $this = this; 
    $this.testMe = function(val){ 
     $scope.myVal = parseInt(val)+1; 
    } 
}]); 

यहाँ की का उपयोग करें 'इस' परीक्षण है:

describe('newDir', function(){ 
var svc, 
    $rootScope, 
    $scope, 
    $controller, 
    ctrl; 


beforeEach(function() { 
    module('myMod'); 
}); 

beforeEach(function() { 
    inject(function (_$controller_,_$rootScope_) { 

     $controller = _$controller_; 
     $rootScope = _$rootScope_; 
     $compile = _$compile_; 
     $scope = $rootScope.$new(); 
     ctrl = $controller('newController', {'$rootScope': $rootScope, '$scope': $scope }); 
    }); 
}); 

it('testMe inc number', function() { 

    ctrl.testMe(10) 
    expect($scope.myVal).toEqual(11); 
}); 

});

Full Code Example

+0

क्या कोई कारण है कि आप '$ this' का उपयोग करते हैं या ऐसा इसलिए है क्योंकि आप 'self'' जैसे चर का उपयोग करना पसंद नहीं करते हैं? – svassr

+0

क्या आप पूछ रहे हैं कि मैंने इसे "स्वयं" नाम देने के बजाय अपना चर "$ यह" क्यों नाम दिया? परिवर्तनीय नामों में मेरी पसंद का कोई कारण नहीं है। "स्वयं" कोड को पढ़ने में आसान बना सकता है। – tmc

+0

हां यह वास्तव में प्रश्न था – svassr

संबंधित मुद्दे