मैं एक दृश्य से दूसरे दृश्य में पारित होने वाले परम का परीक्षण करने में सक्षम होना चाहता हूं। मैं यह देखने के लिए परीक्षण करना चाहता हूं कि क्या परम है और यह कि परम मेरे द्वारा दिए गए नकली परीक्षण डेटा से मेल खाता है।
मैं यूनिट परीक्षणों के लिए काफी नया हूं, सक्रिय मार्ग के संबंध में परीक्षण स्थापित करने और एक परम पास करने में बहुत कुछ पढ़ा है। मुझे लगता है कि जिस बिट पर मैं अटका हुआ हूं वह "उम्मीद" है। यह मुझे एक त्रुटि दे रहा है
Argument of type '{ urn: string[]; }' is not assignable to parameter of type 'Expected<Observable<Params>>'
घटक
export class SearchComponent implements OnInit {
constructor(private route: ActivatedRoute, private router: Router) {
this.getParam();
}
ngOnInit() {
}
getParam():void {
this.route.queryParams.subscribe(params => {
console.log(params["urn"]);
});
}
}
कल्पना
providers: [
HttpClient,
{
provide: ActivatedRoute,
useValue: {
queryParams: of({
urn: '123'
})
}
}
],
...
it('test queryParam in route', () => {
const activatedRoute: ActivatedRoute = fixture.debugElement.injector.get(ActivatedRoute);
activatedRoute.queryParams = of({ urn: '123' });
fixture.detectChanges();
// tick();
expect(activatedRoute.queryParams).toEqual({ urn: ['123'] }); // this line giving me trouble
});
अगर कोई मुझे यह देखने के लिए हाथ दे सकता है कि मैं क्या गलत कर रहा हूँ - यहाँ stackBlitz मैं डेमो के साथ आया था
1 उत्तर
यहां :
expect(activatedRoute.queryParams).toEqual({ urn: ['123'] })
activatedRoute.queryParams
{ urn: ['123'] }
नहीं बल्कि एक Observable
है जो इस मान को सक्रिय करेगा।
आप इसे इस तरह परीक्षण कर सकते हैं:
/*
Notice the extra "done" parameter. It is a function that you must call
to indicate that the test is finished.
It is necessary because we are testing an asynchronous method.
This will prevent the test from exiting until the "done" method is called.
Also, the test will fail if the done method is not called within 5s by default.
*/
it('test queryParam in route', (done) => {
[...]
activatedRoute.queryParams.subscribe((value) => {
expect(value).toEqual({ urn: ['123'] })
done();
})
});
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
angular
Google से वेब फ्रेमवर्क के बारे में प्रश्न इस टैग का उपयोग कोणीय प्रश्नों के लिए करें जो एक व्यक्तिगत संस्करण के लिए विशिष्ट नहीं हैं। पुराने AngularJS (1.x) वेब ढांचे के लिए, कोणीयज टैग का उपयोग करें।
activatedRoute.queryParams
undefined
होता तो आप कॉलबैक के अंदर नहीं पहुंच पाते। मुझे लगता है कि यह त्रुटि कहीं और से आई है। क्या आपके पास लाइन नंबर या कुछ और है जो आपको यह पता लगाने में मदद करता है कि यह त्रुटि कहाँ से आती है? क्या यह.ts
से याspec.ts
से आता है?