{"id":208290,"date":"2025-07-08T12:21:51","date_gmt":"2025-07-08T04:21:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208290\/"},"modified":"2025-07-08T12:21:51","modified_gmt":"2025-07-08T04:21:51","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e6%a8%a1%e6%8b%9f%e4%b8%ad%e6%af%94%e8%be%83-%e5%8c%b9%e9%85%8d%e9%97%ad%e5%8c%85%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208290\/","title":{"rendered":"\u5982\u4f55\u5728\u6a21\u62df\u4e2d\u6bd4\u8f83\/\u5339\u914d\u95ed\u5305\uff1f"},"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>\u5982\u4f55\u5728\u6a21\u62df\u4e2d\u6bd4\u8f83\/\u5339\u914d\u95ed\u5305\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 12:39:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5728\u6a21\u62df\u4e2d\u6bd4\u8f83\/\u5339\u914d\u95ed\u5305\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>tl;dr\uff1a\u6a21\u62df\u65b9\u6cd5\u63a5\u53d7\u95ed\u5305\u3002\u6211\u60f3\u77e5\u9053\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\u5339\u914d\u5668\uff08https:\/\/godoc.org\/github.com\/golang\/mock\/gomock#matcher\uff09\uff1a\u95ed\u5305\u672c\u8eab\u53c8\u4e0e\u79c1\u6709\u7ed3\u6784\u4e00\u8d77\u5de5\u4f5c &#8211; \u8fd9\u610f\u5473\u7740\u6211\u4ec0\u81f3\u65e0\u6cd5\u5728\u6211\u7684\u8fdb\u884c\u6d4b\u8bd5\u4ee5\u68c0\u67e5\u5176\u662f\u5426\u7b26\u5408\u9884\u671f\u3002<\/p>\n<p>\u6211\u6b63\u5728 nlopes\/slack (https:\/\/github.com\/nlopes\/slack) \u7684\u5e2e\u52a9\u4e0b\u4f7f\u7528 slack api \u5f00\u53d1\u4e00\u4e2a\u5c0f\u578b\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4e3a\u4e86\u6d4b\u8bd5\uff0c\u6211\u7528 gomock \u6765\u5632\u7b11 nlopes\/slack\u3002\u4e3a\u6b64\u6211\u521b\u5efa\u4e86\u754c\u9762<\/p>\n<pre>type slackapi interface {\n    openconversation(*slack.openconversationparameters) (*slack.channel, bool, bool, error)\n    postmessage(channelid string, options ...slack.msgoption) (string, string, error)\n    getuserbyemail(email string) (*slack.user, error)\n}<\/pre>\n<p>\u6211\u5728\u6d4b\u8bd5 openconversation \u6216 getuserbyemail \u65f6\u6ca1\u6709\u95ee\u9898\uff0c\u4f8b\u5982<\/p>\n<pre>slackapiclient.\n    expect().\n    getuserbyemail(\"[email&nbsp;protected]\").\n    return(slackuserjohndoe, nil).\n    times(1)<\/pre>\n<p>\u5f53\u6d89\u53ca\u5230 postmessage \u65f6\uff0c\u4e8b\u60c5\u4f1a\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u3002\u5728\u4e3b\u4ee3\u7801\u4e2d\uff0c\u8c03\u7528\u5982\u4e0b\u6240\u793a<\/p>\n<pre>_, _, err := slackapiclient.postmessage(channel.id, slack.msgoptiontext(message, false))<\/pre>\n<p>\u800c slack.msgoptiontext \uff08\u6765\u81ea nlopes\/slack\uff09\u5b9e\u9645\u4e0a\u6b63\u5728\u8fd4\u56de\u95ed\u5305\uff1a<\/p>\n<pre>func msgoptiontext(text string, escape bool) msgoption {\n    return func(config *sendconfig) error {\n        if escape {\n            text = slackutilsx.escapemessage(text)\n        }\n        config.values.add(\"text\", text)\n        return nil\n    }\n}<\/pre>\n<p>\u7531\u4e8e\u65b9\u6cd5\u63a5\u53d7\u95ed\u5305\uff0c\u6211\u9700\u8981\u521b\u5efa\u81ea\u5b9a\u4e49 gomock \u5339\u914d\u5668\uff08https:\/\/godoc.org\/github.com\/golang\/mock\/gomock#matcher\uff09\u3002\u81ea\u5b9a\u4e49\u5339\u914d\u5668\u672c\u8eab\u4e0d\u662f\u95ee\u9898\uff0c\u5b83\u770b\u8d77\u6765\u50cf<\/p>\n<pre>type higherOrderFunctionEqMatcher struct {\n    x interface{}\n}\n\nfunc (e hofEqMatcher) Matches(x interface{}) bool {\n    \/\/return m.x == x\n    return true\n}\n\nfunc (e hofEqMatcher) String(x interface{}) string {\n    return fmt.Sprintf(\"is equal %v\", e.x)\n}<\/pre>\n<p>\u4f46\u662f\uff0c\u7531\u4e8e msgoptiontext \u4f7f\u7528 nlopes\/slack \u79c1\u6709\u7ed3\u6784 sendconfig\uff0c\u6211\u60f3\u77e5\u9053\u5982\u4f55\u5728\u6d4b\u8bd5\u8303\u56f4\u5185\u4f7f\u7528\u5b83\u6765\u68c0\u67e5\u662f\u5426\u4e0e\u671f\u671b\u76f8\u7b49\u3002<\/p>\n<p>\u6211\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u8fd9\u6837\u7684\u95ee\u9898\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8bf7\u8bb0\u4f4f<\/p>\n<ol>\n<li>\u5728 golang \u4e2d\u4f60\u65e0\u6cd5\u6bd4\u8f83\u51fd\u6570<\/li>\n<li>\u5728\u8fd9\u79cd\u7cbe\u786e\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u65e0\u6cd5\u901a\u8fc7\u8c03\u7528\u95ed\u5305\u672c\u8eab\u6765\u8fdb\u884c\u95f4\u63a5\u6d4b\u8bd5\uff08\u56e0\u4e3a\u5b83\u4f7f\u7528\u79c1\u6709\u7684\u7b2c 3 \u65b9\u5e93\u7684\u7ed3\u6784\u4f5c\u4e3a\u53c2\u6570\uff09<\/li>\n<\/ol>\n<p>\u6211\u627e\u5230\u7684\u89e3\u51b3\u65b9\u6848\u662f\u6a21\u62df slack.msgoptiontext(message, false)\uff0c\u5b83\u53c8\u8fd4\u56de postmessage(channelid string, options &#8230;slack.msgoption) \u7684\u95ed\u5305\uff1a<\/p>\n<pre>type slackmsgcreator interface {\n    msgoptiontext(string, bool) slack.msgoption\n}\n\ntype slackmsgcreatorinst struct{}\n\nfunc (s slackmsgcreatorinst) msgoptiontext(text string, escape bool) slack.msgoption {\n    return slack.msgoptiontext(text, escape)\n}<\/pre>\n<p>&#8230;<\/p>\n<pre>slackmsgcreator.\n    expect().\n    msgoptiontext(\"dear john doe, message goes here\", false).\n    return(slack.msgoptiontext(\"dear john doe, message goes here\", false)).\n    times(1)<\/pre>\n<p>\u800c\u4e14\uff0c\u5bf9\u4e8e postmessage &#8211; \u6b63\u5982\u8bc4\u8bba\u4e2d\u6240\u5efa\u8bae\u7684\u90a3\u6837\uff0c\u6211\u552f\u4e00\u53ef\u4ee5\u68c0\u67e5\u7684\u662f\u95ed\u5305\u4e0d\u4e3a\u96f6\uff1a<\/p>\n<pre>slackAPIClient.\n    EXPECT().\n    PostMessage(\"ABCDE\", Not(Nil())).\n    AnyTimes()<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-208290","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208290","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=208290"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208290\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}