हमारे पास कोणीय नियंत्रक में कुछ विधियां हैं, जो स्कोप चर पर नहीं हैं।हम गैर-स्कोप कोणीय नियंत्रक विधियों का परीक्षण कैसे कर सकते हैं?
क्या कोई जानता है, हम जैस्मीन परीक्षणों के अंदर उन तरीकों को कैसे निष्पादित या कॉल कर सकते हैं?
यहां मुख्य कोड है।
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);
});
});
सही। इसके अलावा, हमें निजी कार्यों का परीक्षण नहीं करना चाहिए। आम तौर पर, 'init() 'इकाई को नकली डेटा के साथ परीक्षण किया जाता है जो सुनिश्चित करेगा कि' हैंडलअपऑफएपीआईआई 'या' हैंडलफेलरऑफैपी 'लागू किया गया है। मॉक ऑब्जेक्ट्स/डेटा निष्पादन के पथ को ड्राइव करना चाहिए। – dmahapatro
मैं फ़ंक्शन/परिवर्तनीय सार्वजनिक (जावा की तरह कुछ की तुलना करते समय) बनाने के रूप में $ स्कोप देखता हूं। मैं जावा में यूनिट परीक्षण से अधिक परिचित नहीं हूं, लेकिन क्या आप "निजी कार्यों का परीक्षण नहीं कर रहे हैं" जो आप जावा में सामान्य रूप से करते हैं? – Chris
क्रिस, मेरे पास सामान्य रूप से सीमित लेखन लेखन परीक्षण भी हैं, कुछ जुनीट परीक्षण लिखे हैं लेकिन मैं किसी भी तरह से परीक्षण या किसी भी विशेषज्ञ में विशेषज्ञ नहीं हूं। मैं कल्पना करता हूं क्योंकि निजी कार्यों का खुलासा नहीं होता है जब आप किसी ऑब्जेक्ट के इंस्टेंस को तुरंत चालू करते हैं, तब भी आप एक ही बाधा के तहत हैं कि आप केवल उन चीजों का परीक्षण कर सकते हैं जो सार्वजनिक रूप से सुलभ हैं। मुझे लगता है कि आखिरकार यह समझ में आता है क्योंकि जब आप एक परीक्षा लिखते हैं तो आप ब्लैक बॉक्स फैशन के प्रकार में एक कार्य पूरा करने वाले कोड के एक हिस्से के साथ वास्तव में चिंतित हैं, तो अगर मैं एक्स की आपूर्ति करता हूं तो मैं वाई की अपेक्षा करता हूं, लेकिन कार्यान्वयन विवरण नहीं हैं एक चिंता। – shaunhusain