मैं इस तरह एक नमूना मार्ग है:कोणीय जे एस परीक्षण
angular
.module('appRouter',['ui.router'])
.config(function($stateProvider,$urlRouterProvider){
$stateProvider
.....
.....
.state('settings.account',{
url:'/account',
templateUrl:'templates/account.html',
controller:function(resolveData){
console.log(resolveData);
},
resolve:{
resolveData : function($http){
var root = 'https://jsonplaceholder.typicode.com';
return $http.get(root+'/posts/1').then(function(response){
return response.data;
});
}
}
});
.....
यह सिर्फ एक परीक्षण यूआरएल है जहाँ मैं ऑनलाइन
https://jsonplaceholder.typicode.com/posts/1
मैं राज्य का परीक्षण करना चाहता था।
beforeEach(function(){
module('appRouter');
});
beforeEach(inject(function(_$rootScope_,_$injector_,_$state_,_$httpBackend_,$templateCache){
$rootScope = _$rootScope_;
$state = _$state_;
$injector = _$injector_;
$httpBackend = _$httpBackend_;
$templateCache.put('templates/account.html','');
}));
it('should resolve "resolveData"',function(){
const state = $state.get('settings.account');
const resolveFn = state.resolve.resolveData;
$httpBackend.whenGET('https://jsonplaceholder.typicode.com/posts/1').respond(function(){
return [
200,{
"userId": 1,
"id": 1,
"title": "...",
"body": "..."
}]
});
$httpBackend.expectGET('https://jsonplaceholder.typicode.com/posts/1');
$injector.invoke(resolveFn);
$httpBackend.flush();
expect($injector.annotate(resolveFn)).toEqual(['$http']);
console.log(angular.mock.dump($scope));
expect($scope.resolveData).toEqual({
"userId": 1,
"id": 1,
"title": "...",
"body": "..."
});
लेकिन यह विफल रहता है।
1) should resolve "resolveData"
UI Routerer Config For Account
Expected undefined to equal Object({ userId: 1, id: 1, title: '...', body: '...' }).
at Object.<anonymous> (test/controllers/main-controller-spec.js:114:36)
मैं क्या गलत कर रहा हूं?
अद्यतन
console.log(angular.mock.dump($scope));
देता है निम्नलिखित
कृपया मदद करते हैं।
मुझे लगता है कि मूल त्रुटि के साथ आपके द्वारा पूछे गए मूल प्रश्न (संपादन से पहले) में मूल्य था। यह नया त्रुटि संदेश एक नया सवाल होना चाहिए।वे दोनों अलग-अलग समस्याएं हैं, जिन्हें व्यक्तिगत रूप से संभाला जाना चाहिए। –
मैं समझ नहीं पा रहा हूं कि आप यहां परीक्षण करने की कोशिश कर रहे हैं। मेरा मतलब है: परीक्षणों की जांच करनी चाहिए कि क्या आपका कोड कुछ तर्क कर रहा है, जिसे आप उम्मीद करते हैं। लेकिन इसके बजाय (जहां तक मैं देख सकता हूं) आपकी परीक्षा जांचती है कि क्या आप कुछ अनुरोध से कुछ जेसन प्राप्त कर सकते हैं। परीक्षण का उद्देश्य नहीं है। हो सकता है कि यह सर्वर साइड टेस्ट का अच्छा उद्देश्य हो। इसके बजाए मैं आपको "समाधान" के अंदर कुछ सेवा कॉल करने का सुझाव देता हूं (बस कुछ सेवा इंजेक्शन करके $ http कॉल को प्रतिस्थापित करें और इसकी विधियों में से एक को कॉल करें), अब आप परीक्षण कर सकते हैं कि "state.go" के जवाब में ifs सेवा कहलाती है, और अगर सेवा को सही पैरा के साथ बुलाया जाता है। (मेरी राय है) – happyZZR1400