मेरा एप्लिकेशन ऐसी सेवा का उपयोग करता है जो एक ऐसा वचन देता है जो आमतौर पर अन्य वादों के पूरे समूह पर निर्भर होता है। मैंने इसे परीक्षण (और पठनीयता) को आसान बनाने के लिए अलग-अलग नामित कार्यों में दोबारा प्रतिक्रिया दी है। तो इस मामले में मैं सिर्फ यह जांचना चाहता हूं कि रन फ़ंक्शन अपनी नौकरी करता है और अन्य कार्यों को कॉल करता है।वादा हल होने के बाद फ़ंक्शन को बुलाया जाता है लेकिन जैस्मीन परीक्षण में विफल रहता है। क्यूं कर?
उदा।
run() {
return myService
.connection
.then(this.namedFunction1)
.then(this.namedFunction2)
.then(this.namedFunction3)
.catch((error) => {
console.log("doh!", error.stack);
});
जब मैं परीक्षण namedFunction1 बुलाया गया है कि जैस्मीन विफल रहता है, भले ही यह मामला नहीं है। यह एक छोटा सा कोड उदाहरण मैं चीजों को सरल रखने के लिए बना है:
getString() {
return Promise.resolve("Heeeelp. Heeeelp!!");
}
printToConsole(string) {
console.log(string); // This works! but Jasmine says nay :(
}
myFunction() {
this.getString()
.then(this.printToConsole)
.catch((error) => {
console.log("Some error occurred", error);
});
}
... और परीक्षण:
it("should call the printToConsole function", function() {
spyOn(myClass, "printToConsole").and.callThrough(); //added the call through so it would print
myClass.myFunction();
expect(myClass.printToConsole).toHaveBeenCalled();
});
और उत्पादन ...
> Started F[2016-05-16 11:32:31.898] console - Heeeelp. Heeeelp!!
>
>
> Failures: 1) MyClass myFunction should call the printToConsole
> function Message:
> Expected spy printToConsole to have been called. Stack:
> Error: Expected spy printToConsole to have been called.
मैं मैंने चमेली एसिंच() फ़ंक्शन को जोड़ने की कोशिश की लेकिन यह कुछ भी नहीं किया, और आखिरकार मैं इस वचन को तुरंत उदाहरण में हल कर रहा हूं।
तो यह परीक्षण क्यों विफल हो सकता है?
किसी भी मदद की सराहना की जाएगी। धन्यवाद।
मैंने पहले एसिंक समर्थन का उपयोग करने की कोशिश की, लेकिन मेरा दावा myClass.myFunction()।() कथन के अंदर नहीं था। यह अब और अधिक समझ में आता है। धन्यवाद दोस्त। – spryce