Sabbin karya sun fito a matsayin zaษi mai dacewa da lokaci don masu haษakawa suna gwada lambar su, lokacin da basu da damar zuwa sabar na ainihi. Waษannan sabobin sune ainihin simulations na sabar na ainihi, ana amfani da su wajen haษakawa da gwaji, kuma suna aiki azaman kayan aiki mai mahimmanci a cikin arsenal na ci gaban yanar gizo.
The Magani
Maganin ga matsalar rashin samun damar uwar garken yayin yin codeing ko gwaji sabar karya ce. Don fahimtar wannan mafita, yana da mahimmanci a fahimci rawar uwar garke a cikin haษaka gidan yanar gizon. Sabar shine ainihin mahallin da ke amsa buฦatun daga na'urorin abokin ciniki, tare da martanin da suka dace. Misali, a cikin yanayi na yau da kullun, abokin ciniki na iya aika buฦatu don wani shafin yanar gizon, wanda uwar garken sannan ya ba da.
Koyaya, yayin haษaka lambar, ฦila masu haษakawa ba koyaushe suna samun damar zuwa sabar masu rai ba. Ko saboda rashin kayan aiki ko kuma rashin samun sabar kai tsaye yayin yin codeing, masu haษakawa suna buฦatar hanyar gwada lambar su. Wannan shine lokacin da uwar garken karya ya shigo - yana aiki azaman โsimulatorโ ko โtsayawaโ don sabar ta gaske.
Bayanin mataki-mataki na Code
Don kwatanta yadda a uwar garken karya yana aiki a JavaScript, bari mu yi tafiya ta wasu sauฦaฦan pseudocode.
```JavaScript // First, we need to create an instance of a fake server var server = sinon.fakeServer.create(); // Then, let's stipulate how the server should respond server.respondWith("GET", "/some/endpoint", [200, {}, "Hey there!"]); // We then have the server respond automatically server.respond(); // Lastly, we restore the server to its previous state once we're done testing server.restore(); ```
Bari mu rushe abin da lambar ke yi a cikin tsari-mataki-mataki:
- Mataki na farko yana fara sabar sabar karya ta amfani da Sinon.JS, ฦดan leฦen asirin gwaji, stubs, da izgili ga JavaScript.
- Na gaba, mun ayyana yadda uwar garken zata amsa buฦatun GET zuwa wani wurin ฦarshe.
- Sai mu gaya wa uwar garken don fara amsa buฦatun.
- A ฦarshe, da zarar an gama gwajin mu, za mu mayar da uwar garken don dawo da komai zuwa yanayinsa na asali.
Dakunan karatu da Ayyuka
A cikin snippet code da aka ambata, mun yi amfani da su Sinon.JS, sanannen ษakin karatu don gwajin ฦดan leฦen asiri, stubs, da izgili a cikin JavaScript. Sinon yana ba masu haษaka damar sarrafa ayyuka da halayen su a cikin gwaje-gwajen su. A cikin wannan mahallin, mun yi amfani da shi don ฦirฦirar sabar karya ba tare da matsala ba.
The aiki sinon.fakeServer.create() yana haifar da misalin sabar karya yayin uwar garken.respondDa() hanya ce da ke nuna yadda uwar garken zata amsa duk wani buฦatun mai shigowa. Aikin uwar garken.respond() yana jawo uwar garken don fara amsa buฦatun masu shigowa. Daga karshe, uwar garke.restore() ana amfani da shi don mayar da uwar garken zuwa matsayinsa na asali.
Kyakkyawan uwar garken karya shine cewa ana iya daidaita sigogi don amsawa kamar yadda ake buฦata, ba da damar masu haษakawa su gwada lambar su akan martanin uwar garken iri-iri. Kyakkyawan JavaScript da dakunan karatu irin su Sinon.JS shine cewa suna ฦarfafa masu haษakawa don ฦirฦirar waษannan wuraren gwaji cikin sauฦi da inganci.