{"id":207310,"date":"2025-07-08T08:42:19","date_gmt":"2025-07-08T00:42:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207310\/"},"modified":"2025-07-08T08:42:19","modified_gmt":"2025-07-08T00:42:19","slug":"%e6%b5%8b%e8%af%95golang%e4%b8%ad%e7%9a%84handler%e4%b8%ad%e8%b0%83%e7%94%a8%e8%af%a5%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207310\/","title":{"rendered":"\u6d4b\u8bd5Golang\u4e2d\u7684handler\u4e2d\u8c03\u7528\u8be5\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u6d4b\u8bd5Golang\u4e2d\u7684handler\u4e2d\u8c03\u7528\u8be5\u65b9\u6cd5<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 17:09:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6d4b\u8bd5Golang\u4e2d\u7684handler\u4e2d\u8c03\u7528\u8be5\u65b9\u6cd5\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728 golang \u4e2d\u5b9e\u73b0\u4e00\u4e2a api\u3002\u6211\u6709\u4e00\u4e2a\u7aef\u70b9\uff0c\u6211\u5728\u5176\u4e2d\u8c03\u7528\u5e26\u6709\u5176\u4ed6\u5305\u7684\u53c2\u6570\u7684\u65b9\u6cd5\u3002\u73b0\u5728\u6211\u9700\u8981\u68c0\u67e5\u8bf7\u6c42\u4e2d\u662f\u5426\u5df2\u8c03\u7528\u8be5\u65b9\u6cd5\u3002 <\/p>\n<p>\u4e0b\u9762\u662f\u6211\u6b63\u5728\u505a\u7684\u548c\u6211\u671f\u5f85\u7684\u7c7b\u4f3c\u7684\u5c0f\u573a\u666f\u3002<\/p>\n<h1>\u6211\u7684\u5904\u7406\u7a0b\u5e8f<\/h1>\n<pre>package mypackage\nimport (\n    \"log\"\n    \"github.com\/myrepo\/notifier\" \/\/ my another package\n)\n\nfunc myhandler(writer http.responsewriter, request *http.request) { \n    \/\/ ...\n    \/\/ ...\n\n    notifier.notify(4, \"sdfsdf\")\n\n    \/\/ ...\n    \/\/ ...\n\n}<\/pre>\n<h1>\u6d4b\u8bd5\u5904\u7406\u7a0b\u5e8f<\/h1>\n<pre>func TestMyHandler(t *testing.T) {\n    \/\/ Here I want to \n    req, _ := http.NewRequest(\"GET\", \"\/myendpoint\", nil)\n    \/\/ ... Want to test that notifier.Notify is called\n    \/\/ ...\n}<\/pre>\n<p>\u5728 testmyhandler \u4e2d\uff0c\u6211\u60f3\u68c0\u67e5 <code>notifier.notify<\/code> \u662f\u5426\u5df2\u8c03\u7528\u3002 <\/p>\n<h1>\u8c03\u67e5\u7ed3\u679c<\/h1>\n<p>\u6211\u8bd5\u56fe\u7406\u89e3 assertnumberofcalls\u3001func (*mock) called \u548c func (*mock) methodcalled\uff0c\u4f46\u6211\u4e0d\u786e\u5b9a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec:(\u3002<\/p>\n<p>\u6211\u662f golang \u7684\u65b0\u624b\uff0c\u5bf9\u6b64\u6211\u611f\u5230\u975e\u5e38\u5174\u594b\u3002 \u5982\u679c\u6211\u9057\u6f0f\u4e86\u4efb\u4f55\u5185\u5bb9\uff0c\u8bf7\u544a\u8bc9\u6211\uff0c\u5426\u5219\u60a8\u53ef\u80fd\u9700\u8981\u66f4\u591a\u4fe1\u606f\u624d\u80fd\u66f4\u597d\u5730\u7406\u89e3\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u662f\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u548c\u63a5\u53e3\u7684\u597d\u673a\u4f1a\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u9700\u8981\u63d0\u53d6\u4e00\u4e2a<code>notifier<\/code>\u7684\u6982\u5ff5<\/p>\n<p>\uff08\u8b66\u544a\uff1a\u4ee3\u7801\u672a\u76f4\u63a5\u6d4b\u8bd5\uff09<\/p>\n<pre>type notifier interface {\n    notify(int, string)() error\n}\n<\/pre>\n<p>\u73b0\u5728\uff0c\u4e3a\u4e86\u907f\u514d\u4e0e <code>notifier<\/code> \u5e93\u4ea7\u751f\u4efb\u4f55\u6df7\u6dc6\uff0c\u8bf7\u4f7f\u7528\u672c\u5730\u522b\u540d\u3002<\/p>\n<pre>import \"github.com\/myrepo\/notifier\" mynotifier\n<\/pre>\n<p>\u7136\u540e\uff0c\u56e0\u4e3a\u60a8\u4f7f\u7528\u7684\u5e93\u5c06\u5176\u5bfc\u51fa\u4e3a\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5728\u7ed3\u6784\u4e2d\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0\u6211\u4eec\u7684\u63a5\u53e3\u7684\u7ed3\u6784<\/p>\n<pre>type mynotifier struct {}\n\nfunc (mn *mynotifier) notify(n int, message string) error {\n  return mynotifier.notify(n, message)\n}\n<\/pre>\n<p>\u7136\u540e\u4fee\u6539\u4f60\u7684\u51fd\u6570\uff1a<\/p>\n<pre>func myhandler(writer http.responsewriter, request *http.request, notifier notifier) { \n    \/\/ ...\n    \/\/ ...\n\n    notifier.notify(4, \"sdfsdf\")\n\n    \/\/ ...\n    \/\/ ...\n\n}\n<\/pre>\n<p>\u7136\u540e\u5728\u60a8\u7684\u6d4b\u8bd5\u4e2d\uff0c\u60a8\u73b0\u5728\u53ef\u4ee5\u81ea\u7531\u53d1\u9001\u95f4\u8c0d\u901a\u77e5\u7a0b\u5e8f<\/p>\n<pre>type spynotifier struct {\n  called boolean\n}\n\nfunc (n *spynotifier) notify(n int, msg string) error {\n  n.called = true\n  return\n}<\/pre>\n<p>\u6b64\u65b9\u6cd5\u4e0e \u7c7b\u4f3c\uff0c\u4f46\u4f7f\u7528 \u4e2d\u7684 \u5305\u3002\u5728\u8fd9\u91cc\uff0c\u60a8\u521b\u5efa\u901a\u77e5\u7a0b\u5e8f\u7684\u6a21\u62df\u5b9e\u73b0\uff0c\u6ce8\u518c\u65b9\u6cd5\u8c03\u7528\uff0c\u5e76\u65ad\u8a00\u5df2\u4f7f\u7528\u9884\u671f\u53c2\u6570\u8c03\u7528\u8be5\u65b9\u6cd5\u3002<\/p>\n<h1>\u5b9e\u65bd<\/h1>\n<pre>package handler\n\nimport (\n    \"net\/http\"\n    \"github.com\/stretchr\/testify\/mock\"\n)\n\n\/\/ the interface for the notifier\ntype notifier interface {\n  notify(int, string) error\n}\n\n\/\/ the mock implementation of the interface above\ntype mocknotifier struct {\n  mock.mock\n}\n\n\/\/ stub the notify method to ensure it can be expected later in the test\nfunc (mocknotifier *mocknotifier) notify(arg1 int, arg2 string) error {\n    args := mocknotifier.called(arg1, arg2)\n\n    return args.error(0)\n}\n\n\/\/ this handler which accepts a notifier for dependency injection\ntype handler struct {\n  notifier notifier\n}\n\n\/\/ the myhandler implementation which calls the notifier instance\nfunc (h *handler) myhandler(writer http.responsewriter, request *http.request) {\n  \/\/ this is what we want to test!\n  h.notifier.notify(4, \"sdfsdf\")\n}<\/pre>\n<h1>\u6d4b\u8bd5<\/h1>\n<pre>package handler_test\n\nimport (\n    \"net\/http\"\n    \"net\/http\/httptest\"\n    \"testing\"\n)\n\nfunc TestMyHandler(t *testing.T) {\n  t.Parallel()\n\n  mockNotifier := MockNotifier{}\n  handler := Handler{ notifier: &amp;mockNotifier }\n\n  \/\/ register the mock to expect a call to Notify with the given arguments and return nil as the error\n  mockNotifier.On(\"Notify\", 4, \"sdfsdf\").Return(nil)\n\n  \/\/ setup the test arguments\n  request := httptest.NewRequest(http.MethodGet, \"\/someapi\", nil)\n  writer := httptest.NewRecorder()\n\n  \/\/ call the handler\n  handler.MyHandler(writer, request)\n\n  \/\/ this is the important part!!\n  \/\/ this ensures that the mock Notify method was called with the correct arguments, otherwise the test will fail\n  mockNotifier.AssertExpectations(t)\n}<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u6d4b\u8bd5Golang\u4e2d\u7684handler\u4e2d\u8c03\u7528\u8be5\u65b9\u6cd5\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207310","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207310"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207310\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}