<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ken Chang on Medium]]></title>
        <description><![CDATA[Stories by Ken Chang on Medium]]></description>
        <link>https://medium.com/@justin0915justin0915?source=rss-7d3432e5c862------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OiDh0Qpv3aeC4UY_IclvLQ.jpeg</url>
            <title>Stories by Ken Chang on Medium</title>
            <link>https://medium.com/@justin0915justin0915?source=rss-7d3432e5c862------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 15:37:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@justin0915justin0915/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[隨機產生的字串該如何撰寫單元測試]]></title>
            <link>https://medium.com/@justin0915justin0915/%E9%9A%A8%E6%A9%9F%E7%94%A2%E7%94%9F%E7%9A%84%E5%AD%97%E4%B8%B2%E8%A9%B2%E5%A6%82%E4%BD%95%E6%92%B0%E5%AF%AB%E5%96%AE%E5%85%83%E6%B8%AC%E8%A9%A6-4a2bca214535?source=rss-7d3432e5c862------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a2bca214535</guid>
            <category><![CDATA[chaijs]]></category>
            <category><![CDATA[sinonjs]]></category>
            <category><![CDATA[mocha-js]]></category>
            <dc:creator><![CDATA[Ken Chang]]></dc:creator>
            <pubDate>Sat, 04 Nov 2023 01:39:35 GMT</pubDate>
            <atom:updated>2023-11-04T01:39:35.747Z</atom:updated>
            <content:encoded><![CDATA[<h4>Test with Mocha.js, Chai.js, Sinon.js</h4><blockquote>測試環境</blockquote><pre>&quot;devDependencies&quot;: {<br>    &quot;chai&quot;: &quot;^3.5.0&quot;,<br>    &quot;eslint&quot;: &quot;^3.16.1&quot;,<br>    &quot;mocha&quot;: &quot;^3.2.0&quot;,<br>    &quot;sinon&quot;: &quot;^1.17.7&quot;,<br>  }</pre><p>首先，最近在協助公司做單元測試時遇到的問題，當我在看程式碼時發現有一行由 ${Date.now} (八個數字) + randomHelper 產生的隨機八位大寫字母和數字組成，程式碼如下：</p><pre><br>result.hKey = `${Date.now()}${randomHelper.generator(8, [&#39;uppercase&#39;, &#39;number&#39;])}`;</pre><blockquote>測試思路</blockquote><p>起初，我打算透過 宣告固定generatedHKey 變數，最後returns和generatedHKey一樣的值，程式碼如下：</p><pre>const generatedHKey = &#39;1698982193577&#39;;<br>sandbox.stub(RandomHelper.prototype, &#39;generator&#39;).returns(&#39;1698982193577&#39;)<br>expect(ctx.body.hKey).to.equal(generatedHKey)</pre><p>但當我 npm run test 得到．．．</p><pre>-16989903891712WK03G3Q expect<br>+1698988894589 actual</pre><p>好像沒有得到想要的結果 😔😔😔</p><blockquote>解決辦法</blockquote><p>後來思考到既然無法準確知道隨機產生的字串是什麼，那我是不是可以只測試他的格式就好？</p><p>於是有了以下程式碼：</p><pre>expect(ctx.body.hKey).to.match(/^\d{13}[A-Z0-9]{8}$/);</pre><p>用了正則表達式來解決，會滿足以下條件：</p><ol><li>以 13 位數字（0 到 9）開頭。</li><li>接下來是 8 個字符，這些字符可以是大寫字母（A 到 Z）或數字（0 到 9）。</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a2bca214535" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Event Loop in Javascript]]></title>
            <link>https://medium.com/@justin0915justin0915/event-loop-in-javascript-242fbb175966?source=rss-7d3432e5c862------2</link>
            <guid isPermaLink="false">https://medium.com/p/242fbb175966</guid>
            <category><![CDATA[event-loop-in-js]]></category>
            <dc:creator><![CDATA[Ken Chang]]></dc:creator>
            <pubDate>Thu, 29 Jun 2023 07:14:34 GMT</pubDate>
            <atom:updated>2023-06-29T07:14:34.501Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Javascript 怎麼運作的？</blockquote><p>從下方的圖可以知道，左邊的heap是Javascript儲存記憶體的空間，右邊stack是所有東西的所在位置</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*1DSSzUM5t2h86uqQ2feNSg.png" /></figure><h3>有沒有想過可能有例外的狀況發生？？！！</h3><p>有！當我們用grep抓取像setTimeout, DOM或是發送HTTP請求是不會存放在裡面的</p><blockquote>非同步事件</blockquote><p>當提到非同步事件時，第一個想到的例子不意外是setTimeout，以下舉例：</p><pre>console.log(&quot;Hi&quot;)<br>setTimeout(() =&gt; {<br>  console.log(&#39;非同步操作已完成！&#39;);<br>}, 2000);<br>console.log(&quot;How are you?&quot;)<br><br>// Hi<br>// How are you?<br>// 非同步操作已完成</pre><p>從上述程式碼得知，因為Javascript是屬於單線程的程式語言，一次只能在call stack裡執行一項任務，所以當”Hi”丟進去call stack時，會直接console出來”Hi”，接者把setTimeout丟進去call stack時，因為是屬於回調函式，所以不會在call stack裡面直接做處理，而是會再丟到webAPIs運行，同時call stack會馬上執行”How are you?”並把它印出來。最後，當回調函式在webAPIs完成以後，會再被丟進callback queue裏面存放，直到call stack裡面沒有任何東西了，callback queue裡的處理完的回調函式才會丟回call stack做處理。</p><h4>放個流程圖可能會比較清楚</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*awa8_UFVWCvZfbochr5rTA.png" /></figure><h4>那如果把setTimeout調整成０秒呢？</h4><pre>console.log(&quot;Hi&quot;)<br>setTimeout(() =&gt; {<br>  console.log(&#39;非同步操作已完成！&#39;);<br>}, 0);<br>console.log(&quot;How are you?&quot;)<br><br>// Hi<br>// How are you?<br>// 非同步操作已完成</pre><p>印出來的順序還是一樣的！秒數並不會造成任何影響</p><p>簡單來說，因為回調函式本身不會在call stack裡執行，而是會在webAPIs執行，執行完畢後會把他丟進callback queue做存放，直到call stack清空以後才會執行callback queue裡的回調函式。</p><p>以上是回顧對於event loop的紀錄～</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=242fbb175966" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>