﻿
{"id":9456,"date":"2018-09-23T18:15:50","date_gmt":"2018-09-23T18:15:50","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=9456"},"modified":"2018-09-23T18:15:50","modified_gmt":"2018-09-23T18:15:50","slug":"node-js-unitbirim-test-nedir-nasil-yazilir","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/node-js-unitbirim-test-nedir-nasil-yazilir\/","title":{"rendered":"Node.js &#8211; Unit(Birim) Test Nedir? Nas\u0131l Yaz\u0131l\u0131r?"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Proje s\u00fcre\u00e7lerinde, yap\u0131lacaklarla yap\u0131lanlar aras\u0131ndaki fark\u0131 ortaya \u00e7\u0131karmak i\u00e7in yaz\u0131l\u0131m mimarilerinde belki t\u00fcm s\u00fcre\u00e7lerin en \u00f6nemli noktas\u0131n\u0131 arz eden testing yap\u0131lar\u0131 mevcuttur. Bu yap\u0131lar ki, projedeki birimlere bir kural\/s\u0131n\u0131r\/\u00f6l\u00e7\u00fc tan\u0131makta ve bu birimlerin generate s\u00fcre\u00e7lerinin sonunda bu \u00f6l\u00e7\u00fclere eri\u015fip eri\u015femedi\u011fini denetleyerek yaz\u0131lan kodun ihtiyaca d\u00f6n\u00fck olup olmad\u0131\u011f\u0131n\u0131 de\u011ferlendirmektedirler. \u0130\u015fte biz bu denetleme ve de\u011ferlendirme mekanizmalar\u0131na <em>Unit(Birim) Test<\/em>leri diyece\u011fiz ve bu makalemizde ne olduklar\u0131na ve nas\u0131l olu\u015fturulduklar\u0131na dair detayl\u0131 bir incelemede bulunaca\u011f\u0131z.<\/p>\n<h3>Birim Testi Nedir?<\/h3>\n<p>Bu soruya cevap verebilmek i\u00e7in ilk olarak birim teriminin ne manaya geldi\u011fini a\u00e7\u0131klayal\u0131m. Birim; bir i\u015fi yerine getiren b\u00fct\u00fcn\u00fcn en k\u00fc\u00e7\u00fck par\u00e7as\u0131d\u0131r. Birim testi ise; bir i\u015fi yerine getiren en k\u00fc\u00e7\u00fck par\u00e7as\u0131n\u0131n(yani birimin) do\u011fru \u00e7al\u0131\u015f\u0131p \u00e7al\u0131\u015fmad\u0131\u011f\u0131n\u0131 kontrol etmek i\u00e7in kullan\u0131lan yap\u0131lard\u0131r. Yaz\u0131l\u0131m\u0131n en k\u00fc\u00e7\u00fck birimleri test edildi\u011fi zaman e\u011fer bir s\u0131k\u0131nt\u0131 s\u00f6z konusu de\u011filse bu birimlerden meydana gelecek olan b\u00fct\u00fcnden de beklenilen sonu\u00e7 elde edilecek demektir.<\/p>\n<h3>Birim Testleri Neden Kullan\u0131l\u0131r?<\/h3>\n<p>Bir \u00e7ok nedeninin olmas\u0131n\u0131n yan\u0131nda baz\u0131 m\u00fchim durumlar birim testlerini projenin olmazsa olmaz\u0131 haline getiriyor. S\u00f6zgelimi; bir ekip taraf\u0131ndan in\u015fa edilen projede bir ki\u015finin yapt\u0131\u011f\u0131n\u0131 bir ba\u015fkas\u0131 ister istemez de\u011fi\u015ftirebilmekte ve fark\u0131nda olmaks\u0131z\u0131n kodlar\u0131n i\u015fleyi\u015fini bozabilmektedir. Bu duruma engel olabilmek i\u00e7in yap\u0131lan \u00e7al\u0131\u015fmalara birim testlerinin yaz\u0131lmas\u0131 olas\u0131 de\u011fi\u015fiklikler neticesinde ilgili birimin test sonucundan ge\u00e7emeyece\u011finden dolay\u0131 olas\u0131 hatalar ve i\u015fleyi\u015f bozukluklar\u0131 engellenmi\u015f olacakt\u0131r.<\/p>\n<p>Birim testleri, yaz\u0131ld\u0131klar\u0131 birimleri taraf\u0131m\u0131zca belirlenmi\u015f sonu\u00e7lara ve \u00f6l\u00e7\u00fctlere g\u00f6re de\u011ferlendirmektedirler. Bu de\u011ferlendirmeden ge\u00e7emeyen birimler beklenen icraatlar\u0131n\u0131 yerlerine getiremedi\u011finden dolay\u0131 sistem taraf\u0131ndan kabul edilmeyeceklerdir. Bu durumda yanl\u0131\u015f ya da beklentiye uymayan kodlar\u0131n projeye g\u00f6zden ka\u00e7\u0131p kat\u0131lmas\u0131n\u0131 engellemekte ve s\u00fcre\u00e7te olas\u0131 hatalara kar\u015f\u0131 taraf\u0131m\u0131zca ba\u015ftan \u00f6nlem al\u0131nmas\u0131n\u0131 sa\u011flam\u0131\u015f olmaktad\u0131rlar.<\/p>\n<p>Bir ba\u015fka \u00f6rnek durum ise kusursuz \u00e7al\u0131\u015fan kod \u00fczerinde yap\u0131lacak de\u011fi\u015fiklik neticesinde kodu bozup bozmama korkusunun ya\u015fanmamas\u0131 i\u00e7in birim testleri arac\u0131l\u0131\u011f\u0131yla kodun g\u00fcvenli\u011fi ba\u015ftan sa\u011flanm\u0131\u015f olunacakt\u0131r.<\/p>\n<h3>Birim Testlerinde S\u00fcre\u00e7 Nas\u0131l \u0130\u015fler?<\/h3>\n<p>Projenizi birim testleriyle g\u00fcvenceye almak ve fonksiyonlardan elde edilecek sonu\u00e7lardan beklentiniz do\u011frultusunda emin olmak istiyorsan\u0131z ilk olarak bu beklentilere g\u00f6re \u00f6l\u00e7\u00fctlerinizi olu\u015fturmal\u0131 ve do\u011fal olarak ilk ba\u015fta test kodlar\u0131n\u0131z\u0131 yap\u0131land\u0131rman\u0131z gerekmektedir. Ard\u0131ndan test a\u015famas\u0131 a\u015fa\u011f\u0131daki gibi bir s\u0131rada seyr edecektir;<\/p>\n<ol>\n<li>\u0130lgili testlere uygun fonksiyonlar(birimler) olu\u015fturulur.<\/li>\n<li>\u0130lgili birim testte g\u00f6nderilir.<\/li>\n<li>E\u011fer ki kod compile edilip test a\u015famas\u0131nda \u00f6l\u00e7\u00fctlere ayk\u0131r\u0131 sonu\u00e7lar elde ediliyorsa ilgili kod bu testten ge\u00e7emeyecektir.<\/li>\n<li>Testten ge\u00e7emeyen kod test sonu\u00e7lar\u0131n\u0131n beklentisi do\u011frultusunda yeniden g\u00f6zden ge\u00e7irilecek ve geli\u015ftirilecektir.<\/li>\n<li>Test s\u00fcreci tekrar edecektir ve e\u011fer ki yine testten ge\u00e7emezse 4. ad\u0131m tekrar edecektir.<\/li>\n<li>E\u011fer ilgili kod testi ge\u00e7erse ba\u015far\u0131l\u0131 oldu\u011fu anla\u015f\u0131lacak ve o fonksiyonun tamamland\u0131\u011f\u0131na dair yetkili ki\u015filer taraf\u0131ndan kesin onay verilecektir.<\/li>\n<\/ol>\n<p>Tabi bu s\u00fcrecin getirisi olan ayr\u0131nt\u0131lar da mevcuttur;<\/p>\n<ul>\n<li>Birinin yazd\u0131\u011f\u0131 kodu bir ba\u015fkas\u0131 refactor ederse e\u011fer yap\u0131lan t\u00fcm de\u011fi\u015fiklikler birim testi taraf\u0131ndan onaylanmas\u0131 gerekece\u011fi i\u00e7in kodun g\u00fcvenli\u011fi sa\u011flanm\u0131\u015f olacakt\u0131r.<\/li>\n<li>Yaz\u0131lan kodun birim testinden ge\u00e7ebildi\u011fi s\u00fcre zarf\u0131nca daha k\u0131sa, okunabilir ve kaliteli hale getirilebilme \u015fans\u0131 mevcuttur. S\u0131n\u0131rlar\u0131 ve g\u00fcvenli\u011fi birim testi sa\u011flayaca\u011f\u0131 i\u00e7in analitik d\u00fc\u015f\u00fcnceye daha \u00e7ok enerji sarfedilebilir.<\/li>\n<li>Kod d\u00f6k\u00fcmantasyonu gibi \u00f6znel yetenekleri genelle\u015ftirmeye \u00e7al\u0131\u015fan y\u00f6ntemlere gerek kalmaks\u0131z\u0131n test kodu ile istenilenler daha anla\u015f\u0131l\u0131r bi\u00e7imde izah edilmekte ve bu izah neticesinde er ki\u015fi yaz\u0131l\u0131mc\u0131 kimse onun yo\u011furt yeyi\u015fine g\u00fcvenlierek sonu\u00e7lar g\u00f6zlenmektedir.<\/li>\n<\/ul>\n<blockquote><p>Birim testleri, test s\u00fcre\u00e7lerinin uzamamas\u0131 i\u00e7in performans testlerini kapsamamaktad\u0131rlar.<\/p><\/blockquote>\n<h2>Birim Testi Nas\u0131l Yaz\u0131l\u0131r?<\/h2>\n<p>C#, Java vs. gibi dillerde birim testleri mevcuttur ve her birinde kendine has kurallar\u0131yla ilgili yap\u0131lar olu\u015fturulur. Tabi hangi dilde birim testi olu\u015fturacaksan\u0131z ilgili dile \u00f6zel d\u00f6k\u00fcmanlara g\u00f6z atman\u0131z gerekecektir. Bizler bu makalemizde Node.js mimarisiyle birim testlerinin nas\u0131l yaz\u0131ld\u0131\u011f\u0131n\u0131 de\u011ferlendirece\u011fiz ve konuyu hem teorik hem de pratik olarak a&#8217;dan z&#8217;ye inceleyece\u011fiz.<\/p>\n<h3>\u00d6rnek Node.js Projesinin Aya\u011fa Kald\u0131r\u0131lmas\u0131<\/h3>\n<p>\u0130lk olarak aya\u011fa kald\u0131r\u0131lm\u0131\u015f ortalama bir Node.js projesinin a\u015fa\u011f\u0131daki gibi oldu\u011funu d\u00fc\u015f\u00fcnelim;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst app = require(&quot;express&quot;)();\r\n\/\/Db\r\nrequire(&quot;.\/db\/db&quot;)();\r\n\/\/Routes\r\napp.use(&quot;\/employee&quot;, require(&quot;.\/routers\/employeeRoutes&quot;));\r\napp.listen(4000, () =&gt; console.log(&quot;Yay\u0131n ba\u015far\u0131l\u0131...&quot;));\r\nmodule.exports = app;\r\n<\/pre>\n<p>Burada dikkat ederseniz uygulamam\u0131z\u0131n &#8220;app&#8221; nesnesinide export etmekteyiz. \u00c7\u00fcnk\u00fc birim testlerini server \u00fczerinden \u00e7al\u0131\u015ft\u0131rabilmek i\u00e7in server\u0131 bar\u0131nd\u0131ran &#8220;app&#8221; nesnesine ihtiyac\u0131m\u0131z olacakt\u0131r.<\/p>\n<p>Ayr\u0131ca &#8220;\/employee&#8221; dizinine tan\u0131mlanan &#8220;employeeRoutes&#8221; isimli route dosyas\u0131n\u0131n i\u00e7eri\u011finide a\u015fa\u011f\u0131ya alal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst router = require(&quot;express&quot;).Router();\r\nconst employeeModel = require(&quot;..\/models\/employee&quot;);\r\n\r\nrouter.get(&quot;\/getEmployee&quot;, (request, response, next) =&gt; {\r\n     employeeModel.find({})\r\n          .then(data =&gt; response.json(data))\r\n          .catch(error =&gt; response.send(&quot;Beklenmeyen bir hatayla kar\u015f\u0131la\u015f\u0131ld\u0131. - \/getEmployee -&quot;));\r\n});\r\n\r\nrouter.post(&quot;\/addEmployee&quot;, (request, response, next) =&gt; {\r\n     const employeeInstance = new employeeModel({\r\n          _id: 2,\r\n          name: &quot;Gen\u00e7ay&quot;,\r\n          surName: &quot;Y\u0131ld\u0131z&quot;\r\n     });\r\n\r\n     employeeInstance.save()\r\n          .then(data =&gt; response.json(data))\r\n          .catch(error =&gt; console.log(&quot;Beklenmeyen bir hatayla kar\u015f\u0131la\u015f\u0131ld\u0131. - \/addEmployee -&quot;));\r\n});\r\nmodule.exports = router;\r\n<\/pre>\n<p>Evet&#8230; \u015eimdi bu yap\u0131lanma \u00fczerinde unit testlerimizi yazmaya ba\u015flayaca\u011f\u0131z.<\/p>\n<h3>Test Altyap\u0131s\u0131n\u0131n Sa\u011flanmas\u0131<\/h3>\n<p>Node.js mimarisinde test kodu yazmak i\u00e7in Mocha, Chai ve Chai-Http mod\u00fcllerinin projeye entegre edilmesi gerekmektedir. Bu mod\u00fcllerin g\u00f6revlerini a\u015fa\u011f\u0131da ele al\u0131rsak;<\/p>\n<ul>\n<li><strong>Mocha<\/strong><br \/>\nTest \u00e7at\u0131s\u0131 olarak d\u00fc\u015f\u00fcn\u00fclebilir.<em>(Testing Framework)<\/em><br \/>\nTest s\u00fcrecini ba\u015flatacak ve \u00e7al\u0131\u015ft\u0131racak olan mod\u00fcld\u00fcr.\n<\/li>\n<li><strong>Chai<\/strong><br \/>\nAssertion(<em>iddia, hakk\u0131n\u0131 ispat etme, onaylama<\/em>) i\u015flemlerini yapan mod\u00fcld\u00fcr. Yani test s\u00fcrecinde ilgili birimden yerine getirmesi beklenen \u00f6l\u00e7\u00fctleri belirtmemizi sa\u011flayan yap\u0131y\u0131 sunmaktad\u0131r.\n<\/li>\n<li><strong>Chai-Http<\/strong><br \/>\nChai mod\u00fcl\u00fcn\u00fcn http yap\u0131lanmas\u0131n\u0131 desteklemesi i\u00e7in kullan\u0131lan bir mod\u00fcld\u00fcr.\n<\/li>\n<\/ul>\n<p>\u0130lgili mod\u00fcllerin proje entegrasyonunu sa\u011flamak i\u00e7in a\u015fa\u011f\u0131daki komutu \u00e7al\u0131\u015ft\u0131rman\u0131z yeterlidir.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nnpm install mocha chai chai-http --save\r\n<\/pre>\n<p>Mod\u00fcl entegrasyonundan sonra s\u0131ra test s\u0131n\u0131flar\u0131m\u0131za gelmi\u015f bulunmaktad\u0131r.<\/p>\n<p>Node.js&#8217;de Mocha mod\u00fcl\u00fc test s\u00fcrecini ba\u015flat\u0131rken projenin test s\u0131n\u0131flar\u0131n\u0131 varsay\u0131lan olarak projenin ana dizinindeki &#8220;test&#8221; klas\u00f6r\u00fcnde arayacakt\u0131r. Bu sebeple projemizin ana dizininde &#8220;test&#8221; isimli bir klas\u00f6r olu\u015ftural\u0131m ve ard\u0131ndan ilk test s\u0131n\u0131f\u0131m\u0131z\u0131 i\u00e7erisinde generate edelim.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r..png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r..png\" alt=\"Node.js - Unit(Birim) Test Nedir Nas\u0131l Yaz\u0131l\u0131r.\" width=\"166\" height=\"218\" class=\"aligncenter size-full wp-image-9481\" \/><\/a><br \/>\nDikkat ederseniz hangi s\u0131n\u0131f\/i\u015flem\/birim \u00fczerine test haz\u0131rlayacaksak o s\u0131n\u0131f\u0131n ismini &#8220;.test&#8221; ekiyle birlikte olu\u015fturuyoruz. Buradaki isimlendirme opsiyonel oldu\u011fu i\u00e7in sizlerin keyfine kalm\u0131\u015ft\u0131r. Lakin bu \u015fekilde bir formatland\u0131rma daha anla\u015f\u0131l\u0131r olacakt\u0131r.<\/p>\n<h3>Test Yap\u0131s\u0131n\u0131n Yaz\u0131lmas\u0131<\/h3>\n<p>\u0130lgili test s\u0131n\u0131f\u0131m\u0131za \u00f6ncelikle projeye install etti\u011fimiz &#8220;Chai&#8221; ve &#8220;Chai-Http&#8221; mod\u00fcllerini require edelim ve gerekli ilk ad\u0131mlar\u0131 a\u015fa\u011f\u0131daki gibi olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst chai = require(&quot;chai&quot;);\r\nconst chaiHttp = require(&quot;chai-http&quot;);\r\n\r\nconst should = chai.should();\r\n\/\/Sonucu \u015fu olmal\u0131 gibisinden sorular sorabilmek i\u00e7in...\r\n\r\nchai.use(chaiHttp);\r\n.\r\n.\r\n.\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer &#8220;Chai&#8221; ve &#8220;Chai-Http&#8221; mod\u00fclleri require edilmi\u015ftir. Bunlar\u0131n d\u0131\u015f\u0131nda birim testlerinin uygulanaca\u011f\u0131 fonksiyonlara \u00f6l\u00e7\u00fctleri belirleyebilmek i\u00e7in &#8220;<em>sonucu \u015fu olmal\u0131<\/em>&#8220;, &#8220;<em>sonucu bu olmal\u0131<\/em>&#8221; veya &#8220;<em>sonucu \u015f\u00f6yle olmal\u0131<\/em>&#8221; vs. gibi sorular\u0131 sormam\u0131z\u0131 sa\u011flayan &#8220;should&#8221; fonksiyonundan gelen nesneyide bir de\u011fi\u015fkene atamaktay\u0131z. T\u00fcm bunlar\u0131n d\u0131\u015f\u0131nda &#8220;Chai&#8221; mod\u00fcl\u00fcn\u00fcn http yap\u0131lanmas\u0131n\u0131 sa\u011flayabilmek i\u00e7in &#8220;use&#8221; fonksiyonuna &#8220;Chai-Http&#8221; mod\u00fcl\u00fcn\u00fc atayarak, kullanmas\u0131n\u0131 sa\u011flamaktay\u0131z.<\/p>\n<h4 style=\"color:orange;\">describe Yap\u0131s\u0131 \u0130le Birim Testlerini Gruplama<\/h4>\n<p>Bir test s\u0131n\u0131f\u0131 i\u00e7erisinde birden fazla unit test yaz\u0131labilece\u011fi i\u00e7in i\u015flevsel olarak bu testleri gruplama i\u015flemini &#8220;describe&#8221; yap\u0131s\u0131 ile sa\u011flamaktay\u0131z. Bu noktadan itibaren olu\u015fturaca\u011f\u0131m\u0131z birim testlerini &#8220;describe&#8221; yap\u0131s\u0131 \u00e7er\u00e7evesinde in\u015fa edece\u011fiz. &#8220;describe&#8221; yap\u0131s\u0131n\u0131n prototipi a\u015fa\u011f\u0131daki gibidir.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndescribe(&quot;Comment&quot;, () =&gt; {\r\n     \/\/Testing...\r\n});\r\n<\/pre>\n<p>Yap\u0131lacak testi hangi i\u015fleme \u00f6zel olarak tasarland\u0131\u011f\u0131n\u0131 ifade etmektedir. Burada yap\u0131lacak test olabildi\u011fince detayl\u0131 tarif edilmesi gerekmektedir. Ayriyetten istenildi\u011fi kadar &#8220;describe&#8221; yap\u0131s\u0131 ile test fonksiyonlar\u0131n\u0131z\u0131 gruplayabilirsiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\ndescribe(&quot;1. Test Grubu&quot;, () =&gt; {\r\n     .\r\n     .\r\n     .\r\n});\r\ndescribe(&quot;2. Test Grubu&quot;, () =&gt; {\r\n     .\r\n     .\r\n     .\r\n});\r\n<\/pre>\n<h4 style=\"color:orange;\">it Yap\u0131lar\u0131 \u0130le Birim Testlerinin Yaz\u0131lmas\u0131<\/h4>\n<p>Birim testlerini ger\u00e7ekle\u015ftirdi\u011fimiz yap\u0131lar it yap\u0131lar\u0131d\u0131r. Bu yap\u0131lar describe yap\u0131s\u0131 i\u00e7erisinde tan\u0131mlanmaktad\u0131rlar. Her describe i\u00e7erisinde birden fazla &#8220;it&#8221; yap\u0131s\u0131 olabilmektedir. Yani anlayaca\u011f\u0131n\u0131z describe yap\u0131lar\u0131 it yap\u0131lar\u0131n\u0131 kapsamaktad\u0131rlar.<\/p>\n<p>it yap\u0131lar\u0131n\u0131n prototipini incelersek e\u011fer;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n     it(&quot;Comment&quot;, done =&gt; {\r\n          done();\r\n     });\r\n<\/pre>\n<p>\u015feklinde olacakt\u0131r. Yap\u0131sal olarak &#8220;Comment&#8221; k\u0131sm\u0131 yapt\u0131\u011f\u0131 test i\u015fleminin ne oldu\u011funa dair a\u00e7\u0131klama gerektirmektedir. &#8220;done&#8221; parametresi ise ilgili birimin test neticesinde belirtilen t\u00fcm kriterleri yerlerine getirip testten ge\u00e7ebildi\u011fi durumda testin ba\u015far\u0131yla tamamland\u0131\u011f\u0131n\u0131 ifade etmektedir.<\/p>\n<p>\u015eimdi a\u015fa\u011f\u0131daki \u00f6rne\u011fi inceleyiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ndescribe(&quot;\u00d6rnek Test Uygulamalar\u0131&quot;, () =&gt; {\r\n     it(&quot;x fonksiyonuna get testi&quot;, done =&gt; {\r\n          done();\r\n     });\r\n});\r\n<\/pre>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere describe i\u00e7erisinde it yap\u0131s\u0131 tan\u0131mlanm\u0131\u015ft\u0131r. Burada ki test neticesi ne olursa olsun direkt &#8220;done&#8221; parametresi \u00e7a\u011fr\u0131ld\u0131\u011f\u0131ndan dolay\u0131 bu test ba\u015far\u0131yla tamamlanacak ve ilgili birim testi ge\u00e7mi\u015f olacakt\u0131r.<\/p>\n<p>Biraz daha ger\u00e7ek\u00e7i bir \u00f6rnek yaparsak e\u011fer;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst chai = require(&quot;chai&quot;);\r\nconst chaiHttp = require(&quot;chai-http&quot;);\r\n\r\nconst should = chai.should();\r\n\r\nchai.use(chaiHttp);\r\n\r\nconst server = require(&quot;..\/app&quot;);\r\n\r\ndescribe(&quot;\u00d6rnek Test Uygulamalar\u0131&quot;, () =&gt; {\r\n     it(&quot;GET: employee&quot;, done =&gt; {\r\n          chai.request(server)\r\n               .get(&quot;\/employee\/getEmployee&quot;)\r\n               .end((error, response) =&gt; done());\r\n     });\r\n});\r\n<\/pre>\n<p>\u015feklinde \u00e7al\u0131\u015fma yapabiliriz. Art\u0131k test kodlar\u0131m\u0131z\u0131 ad\u0131m ad\u0131m derinle\u015ftirmekteyiz. Yukar\u0131daki kodu incelerseniz e\u011fer &#8220;chai&#8221; mod\u00fcl\u00fcn\u00fcn &#8220;request&#8221; fonksiyonu server istemektedir. Dolay\u0131s\u0131yla bizde server\u0131m\u0131z olan ana mod\u00fcl\u00fc(app.js) sayfaya require ederek chai mod\u00fcl\u00fcn\u00fcn request fonksiyonuna vermekteyiz.  Bu i\u015flemden sonra ilgili server \u00fczerindeki &#8220;\/employee\/getEmployee&#8221; adresini tetikleyen route fonksiyonuna &#8220;get&#8221; iste\u011fi g\u00f6nderilmekte ve bu istek sonucunu &#8220;end&#8221; fonksiyonu ile elde etmekteyiz. Bu noktada bir hatayla kar\u015f\u0131la\u015f\u0131l\u0131rsa &#8220;error&#8221; parametresi bizlere olas\u0131 hatayla ilgili gerekli bilgileri verecektir. &#8220;response&#8221; parametresi ise hata yoksa bu istek neticesinde d\u00f6n\u00fclen cevab\u0131 getirecektir. Bu test dikkat ederseniz direkt olarak done edilmi\u015ftir. Makalemizde ilerledik\u00e7e ad\u0131m ad\u0131m d\u00f6nen sonu\u00e7ta sorgular\u0131m\u0131z\u0131 ger\u00e7ekle\u015ftirece\u011fiz ve testten ge\u00e7ilebilmesi i\u00e7in \u00f6l\u00e7\u00fctlerimizi belirtece\u011fiz.<\/p>\n<p>Bir describe i\u00e7erisinde birden fazla it olabilece\u011fi gibi, bir it i\u00e7erisinde de birden fazla request durumu s\u00f6z konusu olabilir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst chai = require(&quot;chai&quot;);\r\nconst chaiHttp = require(&quot;chai-http&quot;);\r\n\r\nconst should = chai.should();\r\n\r\nchai.use(chaiHttp);\r\n\r\nconst server = require(&quot;..\/app&quot;);\r\n\r\ndescribe(&quot;\u00d6rnek Test Uygulamalar\u0131&quot;, () =&gt; {\r\n     it(&quot;GET : employee&quot;, done =&gt; {\r\n          chai.request(server)\r\n               .get(&quot;\/employee\/getEmployee&quot;)\r\n               .end((error, response) =&gt; done());\r\n\r\n          chai.request(server).post(&quot;\/employee\/addEmployee&quot;)\r\n               .end((error, response) =&gt; done());\r\n     });\r\n});\r\n<\/pre>\n<p>Yukar\u0131da oldu\u011fu gibi&#8230;<\/p>\n<h3 style=\"color:orange;\">Yaz\u0131lan Birim Testlerinin \u00c7al\u0131\u015ft\u0131r\u0131l\u0131p Denetlenmesi<\/h3>\n<p>T\u00fcm birim testleri yaz\u0131l\u0131p tamamland\u0131ktan sonra art\u0131k s\u0131ra bu testlerin i\u015fletilmesine gelmi\u015f bulunmaktad\u0131r. Bu i\u015flem i\u00e7in projemizin package.json dosyas\u0131na gelerek &#8220;scripts&#8221; k\u0131sm\u0131nda a\u015fa\u011f\u0131daki gibi alan olu\u015fturularak Mocha mod\u00fcl\u00fc komutland\u0131r\u0131lmal\u0131d\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.  \r\n&quot;scripts&quot;: {\r\n    &quot;test&quot;: &quot;mocha&quot;\r\n  },\r\n.\r\n.\r\n.  \r\n<\/pre>\n<p>ve bu i\u015flemden sonra projemizde<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nnpm run test\r\n<\/pre>\n<p>komutunu \u00e7al\u0131\u015ft\u0131rmam\u0131z &#8220;test&#8221; klas\u00f6r\u00fc alt\u0131ndaki t\u00fcm birim testlerinin denetlenmesi i\u00e7in yeterli olacakt\u0131r.<\/p>\n<p>Testing \u00f6rneklendirmesi i\u00e7in a\u015fa\u011f\u0131daki birim testlerini ele alabiliriz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst chai = require(&quot;chai&quot;);\r\nconst chaiHttp = require(&quot;chai-http&quot;);\r\n\r\nconst should = chai.should();\r\n\r\nchai.use(chaiHttp);\r\n\r\nconst server = require(&quot;..\/app&quot;);\r\n\r\ndescribe(&quot;\u00d6rnek Test Uygulamalar\u0131&quot;, () =&gt; {\r\n     it(&quot;GET : employee&quot;, done =&gt; {\r\n          chai.request(server)\r\n               .get(&quot;\/employee\/getEmployee&quot;)\r\n               .end((error, response) =&gt; done());\r\n     });\r\n\r\n     it(&quot;POST : employee&quot;, done =&gt; {\r\n          chai.request(server)\r\n               .get(&quot;\/employee\/addEmployee&quot;)\r\n               .end((error, response) =&gt; done());\r\n     });\r\n});\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-1.png\" alt=\"Node.js - Unit(Birim) Test Nedir Nas\u0131l Yaz\u0131l\u0131r.\" width=\"478\" height=\"339\" class=\"aligncenter size-full wp-image-9510\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-1.png 478w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-1-300x213.png 300w\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" \/><\/a><\/p>\n<p>Yukar\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcne dikkat ederseniz e\u011fer yapm\u0131\u015f oldu\u011fumuz testing i\u015flemi neticesinde ilgili testing s\u00fcreci sonland\u0131r\u0131lmamaktad\u0131r. Bunun i\u00e7in package.json dosyas\u0131nda a\u015fa\u011f\u0131daki ayar\u0131n yap\u0131lmas\u0131 gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\n  &quot;scripts&quot;: {\r\n    &quot;test&quot;: &quot;mocha --exit&quot;\r\n  }\r\n.\r\n.\r\n.\r\n<\/pre>\n<p>&#8220;mocha&#8221; komutunun yan\u0131na eklenen &#8220;&#8211;exit&#8221; komutu sayesinde testing i\u015flemi yap\u0131ld\u0131ktan sonra test modundan otomatik \u00e7\u0131k\u0131lacak dolay\u0131s\u0131yla bu moddan \u00e7\u0131kmak i\u00e7in extradan CTRL + C kombinasyonuyla bizlerin enerji sarf etmesine gerek kalmayacakt\u0131r.<\/p>\n<p>Evet&#8230; G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere testten ba\u015far\u0131yla ge\u00e7en metotlar yukar\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde oldu\u011fu gibi onaylanmaktad\u0131r. Testen ge\u00e7emeyen metotlar ise uyar\u0131lacakt\u0131r. Bununla ilgili yaz\u0131m\u0131z\u0131n ilerleyen k\u0131s\u0131mlar\u0131nda \u00f6rneklendirmeler yapaca\u011f\u0131z.<\/p>\n<p>Mocha mod\u00fcl\u00fc ile ilgili bir ba\u015fka bilgi vermemiz gerekirse e\u011fer; testing s\u00fcrecinde sade ve sadece ana dizindeki &#8220;test&#8221; klas\u00f6r\u00fc i\u00e7erisindeki test s\u0131n\u0131flar\u0131n\u0131 tarad\u0131\u011f\u0131n\u0131 ve i\u00e7lerindeki tan\u0131mlanm\u0131\u015f birim testlerini \u00e7al\u0131\u015ft\u0131rd\u0131\u011f\u0131n\u0131 s\u00f6ylemi\u015ftik. E\u011fer ki, &#8220;test&#8221; klas\u00f6r\u00fc i\u00e7erisinde alt klas\u00f6rler ile test s\u0131n\u0131flar\u0131n\u0131z\u0131 kategorize etmek istiyorsan\u0131z, Mocha mod\u00fcl\u00fcn\u00fcn bu klas\u00f6rler i\u00e7erisinde bulunacak test s\u0131n\u0131flar\u0131na eri\u015febilmesi i\u00e7inde yine package.json dosyas\u0131nda a\u015fa\u011f\u0131daki ayar\u0131n yap\u0131lmas\u0131 gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\n  &quot;scripts&quot;: {\r\n    &quot;test&quot;: &quot;mocha --recursive&quot;\r\n  }\r\n.\r\n.\r\n.\r\n<\/pre>\n<p>&#8220;&#8211;recursive&#8221; komutu ile Mocha mod\u00fcl\u00fc t\u00fcm alt klas\u00f6rlerin i\u00e7ini tarayacak ve t\u00fcm test s\u0131n\u0131flar\u0131na eri\u015ferek \u00e7al\u0131\u015ft\u0131racakt\u0131r.<\/p>\n<h3 style=\"color:orange;\">Birim Testlerinde Should \u0130le \u00d6l\u00e7\u00fctlerin\/S\u0131n\u0131rlar\u0131n Belirlenmesi<\/h3>\n<p>Testing i\u015flemine tabi tutulacak birimlerin i\u015flevsellikleri neticesinde hangi s\u0131n\u0131rlarda de\u011ferler d\u00f6nd\u00fcrece\u011fine dair \u00f6l\u00e7\u00fctlerin belirlenmesi i\u00e7in &#8220;Chai&#8221; mod\u00fcl\u00fcn\u00fcn &#8220;should&#8221; fonksiyonundan gelen nesneyi kullanaca\u011f\u0131m\u0131z\u0131 belirtmi\u015ftik.<\/p>\n<p>Bu nesne sayesinde it yap\u0131s\u0131 i\u00e7erisinde teste tabi tutulan fonksiyonun geri d\u00f6n\u00fc\u015f kriterlerini a\u015fa\u011f\u0131daki gibi belirtebilmekteyiz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst chai = require(&quot;chai&quot;);\r\nconst chaiHttp = require(&quot;chai-http&quot;);\r\n\r\nconst should = chai.should();\r\n\r\nchai.use(chaiHttp);\r\n\r\nconst server = require(&quot;..\/app&quot;);\r\n\r\ndescribe(&quot;\u00d6rnek Test Uygulamalar\u0131&quot;, () =&gt; {\r\n     it(&quot;GET : employee&quot;, done =&gt; {\r\n          chai.request(server)\r\n               .get(&quot;\/employee\/getEmployee&quot;)\r\n               .end((error, response) =&gt; {\r\n                    response.should.have.status(200);\r\n                    \/\/Bu request neticesinde status kod 200 olmal\u0131d\u0131r.\r\n                    done();\r\n               });\r\n     });\r\n});\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi &#8220;Chai&#8221; mod\u00fcl\u00fc arac\u0131l\u0131\u011f\u0131yla ilgili route&#8217;a yap\u0131lan request neticesinde &#8220;get&#8221;<span style=\"font-size:14px;\">(ya da post, put veya delete&#8217;de olabilirdi)<\/span> talebi neticesinde d\u00f6nen sonucun &#8220;should &#8211; have&#8221; yap\u0131s\u0131 ile kriterini belirlemi\u015f bulunmaktay\u0131z.<\/p>\n<p>Burada &#8220;should&#8221;; <em>mal\u0131, meli, gerekmeli<\/em> manas\u0131na gelirken, &#8220;have&#8221;; <em>sahip<\/em> anlam\u0131na gelmektedir. Yani &#8220;status kodu 200&#8217;e <em style=\"color:gray;\">sahip olmal\u0131<\/em>&#8221; manas\u0131nda bir kriter belirlemi\u015f bulunmaktay\u0131z. E\u011fer ki ilgili birim bu kriterden ge\u00e7ebilirse done fonksiyonu \u00e7a\u011fr\u0131lacak ve test ba\u015far\u0131yla tamamlanacakt\u0131r.<\/p>\n<p>T\u00fcm bunalr\u0131n d\u0131\u015f\u0131nda ilgili birimin i\u015flevi neticesinde geriye d\u00f6nd\u00fc\u011f\u00fc de\u011ferden tutun, de\u011fer tipine ve hatta gelen veri obje ise property bilgisine kadar t\u00fcm bilgileri kriter olarak belirleyebilmekteyiz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst chai = require(&quot;chai&quot;);\r\nconst chaiHttp = require(&quot;chai-http&quot;);\r\n\r\nconst should = chai.should();\r\n\r\nchai.use(chaiHttp);\r\n\r\nconst server = require(&quot;..\/app&quot;);\r\n\r\ndescribe(&quot;\u00d6rnek Test Uygulamalar\u0131&quot;, () =&gt; {\r\n     it(&quot;POST : employee&quot;, done =&gt; {\r\n          chai.request(server)\r\n               .post(&quot;\/employee\/addEmployee&quot;)\r\n               .end((error, response) =&gt; {\r\n                    response.should.have.status(200);\r\n                    \/\/Bu request neticesinde status kod 200 olmal\u0131d\u0131r.\r\n                    response.body.should.be.a(&quot;object&quot;);\r\n                    \/\/Gelen veri bir obje olmal\u0131d\u0131r.\r\n                    response.body.should.be.property(&quot;name&quot;);\r\n                    \/\/Obje i\u00e7erisinde name ad\u0131nda bir property olmal\u0131d\u0131r.\r\n                    response.body.should.be.property(&quot;name&quot;).eql(&quot;Gen\u00e7ay&quot;);\r\n                    \/\/Obje i\u00e7erisinde name ad\u0131ndaki propertynin de\u011feri &quot;Gen\u00e7ay&quot; de\u011ferine e\u015fit olmal\u0131d\u0131r.\r\n                    done();\r\n               });\r\n     });\r\n});\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer &#8220;post&#8221; fonksiyonu neticesinde gelen sonu\u00e7 \u00fczerinde kriterlerimizi belirlemi\u015f bulunmaktay\u0131z. Burada &#8220;should&#8221;; <em>meli, mal\u0131<\/em>, &#8220;be&#8221;; <em>olmak<\/em> ve &#8220;a&#8221;; <em>herhangi bir<\/em> manas\u0131na gelmektedir.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-2.png\" alt=\"Node.js - Unit(Birim) Test Nedir Nas\u0131l Yaz\u0131l\u0131r.\" width=\"404\" height=\"284\" class=\"aligncenter size-full wp-image-9530\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-2.png 404w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-2-300x211.png 300w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><\/a><br \/>\nG\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi ilgili route&#8217;ta bulunan fonksiyonumuz t\u00fcm kriterleri sa\u011flayarak testten ba\u015far\u0131yla ge\u00e7mi\u015f bulunmaktad\u0131r.<\/p>\n<p>E\u011fer ki bir metot herhangi bir kriteri sa\u011flamazsa testing a\u015famas\u0131nda onay alamayaca\u011f\u0131 i\u00e7in sistem taraf\u0131ndan otamatik uyar\u0131lacakt\u0131r ve o birimin g\u00f6revini yerine getirmedi\u011fi anla\u015f\u0131lacakt\u0131r. \u00d6rne\u011fin, yukar\u0131daki test kodunun status kriterini 400 olarak belirtirsek e\u011fer;<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-3.png\" alt=\"Node.js - Unit(Birim) Test Nedir Nas\u0131l Yaz\u0131l\u0131r.\" width=\"726\" height=\"801\" class=\"aligncenter size-full wp-image-9534\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-3.png 726w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-3-272x300.png 272w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/a>bizlere ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde oldu\u011fu gibi &#8220;<em>sen status kodunu 400 istiyorsun ama 200 sonucu geliyor<\/em>&#8221; \u015feklinde hata verecektir. Bu \u015fekilde test s\u00fcrecinden ge\u00e7emeyen t\u00fcm metotlar bildirilecektir.<\/p>\n<h3 style=\"color:orange;\">before Yap\u0131s\u0131 \u0130le Test \u00d6nceli\u011fi<\/h3>\n<p>Test s\u0131n\u0131flar\u0131nda t\u00fcm test i\u015flemlerinden \u00f6nce i\u015flem yapabilmek i\u00e7in tetiklenen before yap\u0131s\u0131 mevcuttur.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst chai = require(&quot;chai&quot;);\r\nconst chaiHttp = require(&quot;chai-http&quot;);\r\n\r\nconst should = chai.should();\r\n\r\nchai.use(chaiHttp);\r\n\r\nconst server = require(&quot;..\/app&quot;);\r\n\r\ndescribe(&quot;\u00d6rnek Test Uygulamalar\u0131&quot;, () =&gt; {\r\n     it(&quot;\u00d6rnek Test 1&quot;, done =&gt; {\r\n          console.log(&quot;\u00d6rnek Test 1&quot;);\r\n          done();\r\n     });\r\n\r\n     it(&quot;\u00d6rnek Test 2&quot;, done =&gt; {\r\n          console.log(&quot;\u00d6rnek Test 2&quot;);\r\n          done();\r\n     });\r\n\r\n     before(done =&gt; {\r\n          console.log(&quot;Her\u015feyden \u00f6nce ben \u00e7al\u0131\u015f\u0131r\u0131m&quot;);\r\n          done();\r\n     });\r\n});\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/09\/Node.js-UnitBirim-Test-Nedir-Nas\u0131l-Yaz\u0131l\u0131r.-4.png\" alt=\"Node.js - Unit(Birim) Test Nedir Nas\u0131l Yaz\u0131l\u0131r.\" width=\"217\" height=\"97\" class=\"aligncenter size-full wp-image-9539\" \/><\/a><br \/>\nBu yap\u0131 sayesinde test i\u00e7in gerekli \u015fartlar\u0131 \u00f6ncelikle yerine getirebilmekte ve ard\u0131ndan test s\u00fcrecine ba\u015flayabilmekteyiz.<\/p>\n<h3 style=\"color:orange;\">Authentication Gerektiren Birimlere Testing Esnas\u0131nda Token Temini<\/h3>\n<p>E\u011fer ki, test edilecek birimler authentication ile kimlik do\u011frulama gerektirmekteyseler bunun i\u00e7in ilk olarak gerekli do\u011frulaman\u0131n sa\u011flanm\u0131\u015f olmas\u0131 gerekmektedir. Dolay\u0131s\u0131yla test s\u00fcrecinde kullan\u0131lmak \u00fczere gerekli token vs. gibi yap\u0131lar\u0131n test s\u00fcrecinden \u00f6nce temin edilmesi gerekmektedir.<\/p>\n<p>Bunun i\u00e7inde bir \u00fcstte bahsetti\u011fimiz before yap\u0131s\u0131 arac\u0131l\u0131\u011f\u0131yla test ba\u015flamadan \u00f6nce token de\u011ferlerimizi elde edecek, ard\u0131ndan testing s\u00fcrecini ba\u015flataca\u011f\u0131z.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst chai = require(&quot;chai&quot;);\r\nconst chaiHttp = require(&quot;chai-http&quot;);\r\n\r\nconst should = chai.should();\r\n\r\nchai.use(chaiHttp);\r\n\r\nconst server = require(&quot;..\/app&quot;);\r\n\r\nlet token, status;\r\ndescribe(&quot;Token Test Uygulamalar\u0131&quot;, () =&gt; {\r\n     before(done =&gt; {\r\n          chai.request(server)\r\n               .post(&quot;\/getToken&quot;)\r\n               .send({ userName: &quot;gncy&quot;, password: 12345 })\r\n               .end((error, response) =&gt; {\r\n                    if (!error) {\r\n                         token = response.body.token;\r\n                         status = response.body.status;\r\n                         console.log(`Token : ${token}\\nStatus : ${status}`);\r\n                    } else\r\n                         console.log(&quot;Hata al\u0131nd\u0131&quot;);\r\n                    done();\r\n               });\r\n     });\r\n\r\n     it(&quot;Employee Get&quot;, done =&gt; {\r\n          console.log(token);\r\n          console.log(status);\r\n          chai.request(server)\r\n               .get(&quot;\/employee\/getEmployee&quot;)\r\n               .set(&quot;x-access-token&quot;, token)\r\n               .end((error, response) =&gt; {\r\n                    response.should.have.status(200);\r\n                    response.body.should.be.a(&quot;array&quot;);\r\n\r\n                    done();\r\n               });\r\n     });\r\n});\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer test a\u015famas\u0131nda authentication gerektiren birimler ni\u00e7in nas\u0131l token talep edildi\u011fini ve talep edilen token\u0131n testing s\u00fcrecinde kullan\u0131ld\u0131\u011f\u0131n\u0131 g\u00f6rm\u00fc\u015f olacaks\u0131n\u0131z.<\/p>\n<p>Evet&#8230;<br \/>\nUnit Testler \u00fczerine olan makalemizin sonuna gelmi\u015f bulunmaktay\u0131z. \u0130\u00e7erik olarak biraz uzun ve detayl\u0131 anlat\u0131m sergilemi\u015f olabilirim. Elimden geldi\u011fince m\u00fcmk\u00fcn mertebe her hususa de\u011finip, de\u011ferlendirmek istedim.<\/p>\n<p>Zahmet edip okuyanlara \u015fimdiden te\u015fekk\u00fcr ederim&#8230;<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Proje s\u00fcre\u00e7lerinde, yap\u0131lacaklarla yap\u0131lanlar aras\u0131ndaki fark\u0131 ortaya \u00e7\u0131karmak i\u00e7in yaz\u0131l\u0131m mimarilerinde belki t\u00fcm s\u00fcre\u00e7lerin en \u00f6nemli noktas\u0131n\u0131 arz eden testing yap\u0131lar\u0131 mevcuttur. Bu yap\u0131lar ki, projedeki birimlere bir kural\/s\u0131n\u0131r\/\u00f6l\u00e7\u00fc tan\u0131makta ve bu birimlerin&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":8600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2145],"tags":[2631,2640,2641,2647,2633,2636,2637,2638,2639,2635,2646,2642,2645,2644,2632,2649,2643,2634,2630,2648],"class_list":["post-9456","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","tag-birim-test","tag-mocha-exit","tag-mocha-recursive","tag-node-js-before","tag-node-js-birim-test","tag-node-js-chai","tag-node-js-chai-http","tag-node-js-describe","tag-node-js-it","tag-node-js-mocha","tag-node-js-shoild-be-a","tag-node-js-should","tag-node-js-should-be-property","tag-node-js-should-have","tag-node-js-unit-test","tag-node-js-unit-test-get-token","tag-should","tag-testing-framework","tag-unit-test","tag-unit-test-before"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=9456"}],"version-history":[{"count":84,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9456\/revisions"}],"predecessor-version":[{"id":9546,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9456\/revisions\/9546"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/8600"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=9456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=9456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=9456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}