# 11.มาเรียนการใช้ Moq กันเถอะ

💬 ในรอบนี้เราจะมาลองใช้ Moq กันดูนะครับว่ามันจะมาช่วยจัดการกับงานส่วนอื่นๆที่เราไม่สนใจยังไง

{% embed url="<https://www.youtube.com/watch?v=NWTfG7XBBRQ&list=PLUjAn8nwWniiL3ToFK8PfmAo8U6IoGAkg&index=13>" %}

{% embed url="<https://www.youtube.com/watch?v=1qMLUvK58Kw&list=PLUjAn8nwWniiL3ToFK8PfmAo8U6IoGAkg&index=14>" %}

{% embed url="<https://www.youtube.com/watch?v=P9Hfn8wWzDQ&list=PLUjAn8nwWniiL3ToFK8PfmAo8U6IoGAkg&index=15>" %}

{% embed url="<https://www.youtube.com/watch?v=YobFf8ih78o&list=PLUjAn8nwWniiL3ToFK8PfmAo8U6IoGAkg&index=16>" %}

## 🎯 สรุปสั้นๆ

### 👨‍🚀 หลักการใช้ mock framework

จริงๆมันไม่เกี่ยวกับ mock framework หรอก แต่มันเป็นหลักในการออกแบบคือ เราควรพยายามหลีกเลี่ยงการที่ class จะต้องไปผูกกันกับ concrete class โดยเราควรให้ class มันรู้จักกันแค่ในมุม abstraction มากกว่า

{% hint style="info" %}
ถ้าอ่านแล้วมึนๆผมขอพูดง่ายๆว่า เราควรเรียกใช้ class อื่นโดยอ้างถึง **interface** แทนที่จะเรียกใช้ class นั้นตรงๆนะครับ\
เช่น จากเดิมเราเก็บ object ของเครื่องคิดเลขเป็นแบบนี้\
Calculator cal;\
เราควรเปลี่ยนมาเป็นแบบนี้\
ICalculator cal; (***ICalculator เป็น interface นะครับ)***
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.saladpuk.com/software-testing/tdd101/moq101.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
