## Mock
patch和patch.object用于mock功能。
这两个函数的使用方法于unittest.mock的patch和patch.object一致。


**区别**:
1. 无需更改代码即可开启和关闭mock，参考[`<config>`](../local/xml.md#config)的@enable-mock配置
2. unittest.mock的patch和patch.object装饰在TestCase类或方法上时，它会装饰方法并且强制指定参数。
   而在ngta里面，测试方法的参数不是必须的，可以提供，也可以不提供。


**示例如下**：
```python
# encoding: utf-8

import requests
from ngta import TestCase, test
from ngta.mock import patch


def gen_response(status_code, content):
    resp = requests.Response()
    resp.status_code = status_code
    resp._content = content
    return resp


MOCK_RESP_CONTENT = b"""
{ "store": {
    "book": [
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}
"""

class Test(TestCase):
    @patch("requests.get", return_value=gen_response(200, MOCK_RESP_CONTENT))
    def test_1111(self):
        resp = requests.get("http://www.baidu.com")
        print(resp, resp.text)

    def test_2222(self):
        patcher = patch("requests.get", return_value=gen_response(200, MOCK_RESP_CONTENT))
        patcher.start()
        resp = requests.get("http://www.baidu.com")
        print(resp, resp.text)
        patcher.stop()

    def test_3333(self):
        with patch("requests.get", return_value=gen_response(200, MOCK_RESP_CONTENT)):
            resp = requests.get("http://www.baidu.com")
            print(resp, resp.text)
```