{"id":2539,"date":"2021-10-12T01:33:27","date_gmt":"2021-10-11T17:33:27","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=2539"},"modified":"2022-02-16T17:19:49","modified_gmt":"2022-02-16T09:19:49","slug":"nsubstitute","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2021\/10\/12\/nsubstitute\/","title":{"rendered":"D-03-\u7522\u751f\u6a21\u64ec\u8cc7\u6599 ? NSubstitute"},"content":{"rendered":"<h1>\u5982\u4f55\u6a21\u64ec\u8cc7\u6599<\/h1>\n<p>\u5728\u4e0a\u4e00\u7bc7\u4ecb\u7d39\u5b8cBDD\u4ee5\u53ca\u5982\u4f55\u5728VSCode\u64b0\u5bebBDD\u7684\u6e2c\u8a66\u6848\u4f8b\u4ee5\u53caStep\u4e4b\u5f8c\uff0c\u76f8\u4fe1\u5927\u5bb6\u5728\u64b0\u5beb\u6e2c\u8a66\u6848\u4f8b\u6642\u6642\u5e38\u6703\u9047\u5230\u76f8\u4f9d\u7684\u4ecb\u9762\u4e0d\u77e5\u9053\u8a72\u5982\u4f55\u8655\u7406\uff0c\u518d\u4f86\u7b97\u662f\u77e5\u9053\u4e86Mock\uff0cFake\u4e4b\u5f8c\u4e5f\u4e0d\u77e5\u9053\u8a72\u5728\u6e2c\u8a66\u7684\u7a0b\u5f0f\u4e2d\u600e\u9ebc\u505a\uff0c\u6240\u4ee5\u4eca\u5929\u8ddf\u5927\u5bb6\u4ecb\u7d39C#\u7684NSubstitute\uff0c\u53ef\u4ee5\u6a21\u64ec\u4ecb\u9762\u7684\u65b9\u6cd5\u63a5\u6536\u53c3\u6578\u4e26\u4e14\u8fd4\u9084\u8cc7\u6599\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u6a21\u64ec\u8cc7\u6599<\/h2>\n<p>\u300c\u524d\u8f29\uff0c\u6628\u5929\u63d0\u7684\u6e2c\u8a66\u6848\u4f8b\u6211\u9047\u5230\u554f\u984c\u4e86\uff0c\u6211\u8981\u6e2c\u8a66\u7684\u5c0d\u8c61\u6709\u76f8\u4f9d\u5143\u4ef6\uff0c\u6211\u8a72\u5982\u4f55\u7522\u751f\u9019\u4e9b\u5143\u4ef6\u4f86\u8ddf\u6211\u7684\u6e2c\u8a66\u5c0d\u8c61\u4e92\u52d5\u3002\u300d<br \/>\n\u4e00\u5927\u65e9\u5c0f\u5149\u554f\u5927\u982d\u95dc\u65bc\u6e2c\u8a66\u7684\u554f\u984c\uff0c\u9019\u6642\u5927\u982d\u4f3c\u4e4e\u4e5f\u5728\u5beb\u6e2c\u8a66\u6848\u4f8b\u4e0a\u9047\u5230\u554f\u984c\u3002<br \/>\n\u300c\u6069\uff0c\u6211\u4e5f\u9047\u5230\u4e00\u4e9b\u554f\u984c\uff0c\u4e0d\u904e\u6211\u9047\u5230\u7684\u662f\u6211\u5982\u4f55\u9a57\u8b49\u6c92\u6709\u56de\u50b3\u503c\u7684\u6e2c\u8a66\u5c0d\u8c61\uff0c\u4e0d\u7136\u6211\u5011\u4e00\u8d77\u53bb\u554f\u8001K\u524d\u5099\u5427\u3002\u300d<br \/>\n\u6240\u4ee5\u4ed6\u5011\u5169\u500b\u5c31\u4e00\u8d77\u53bb\u627e\u8001K\u8a62\u554f\u95dc\u65bc\u64b0\u5beb\u6e2c\u8a66\u6848\u4f8b\u6642\u9047\u5230\u7684\u554f\u984c\u3002<br \/>\n\u300c\u597d\u7684\uff0c\u4f60\u5011\u9047\u5230\u7684\u662f\u5982\u4f55\u6a21\u64ec\u8cc7\u6599\u4f86\u5b8c\u6210\u4f60\u5011\u7684\u6e2c\u8a66\u3002\u4e0d\u904e\u5728\u544a\u8a34\u4f60\u5982\u4f55\u505a\u4e4b\u524d\u5148\u544a\u8a34\u4f60\u5011\u4e00\u4e9b\u76f8\u95dc\u77e5\u8b58\uff0c\u56e0\u70ba\u9019\u6a23\u4ee5\u5f8c\u5728\u8aaa\u660e\u4e0a\u6703\u6bd4\u8f03\u65b9\u4fbf\u3002\u300d<\/p>\n<h3>\u6e2c\u8a66\u4e2d\u7684\u540d\u8a5e<\/h3>\n<p>\u9019\u908a\u6709\u4e9b\u6e2c\u8a66\u6642\u7684\u5c08\u6709\u540d\u8a5e\u8981\u8ddf\u5927\u5bb6\u4ecb\u7d39\u4e00\u4e0b\uff0c\u9019\u662f\u7531<a href=\"https:\/\/en.wikipedia.org\/wiki\/Martin_Fowler_(software_engineer)\" title=\"Martin Fowler\" rel=\"nofollow noopener\" target=\"_blank\">Martin Fowler<\/a>\u7684\u6587\u7ae0<a href=\"https:\/\/martinfowler.com\/bliki\/TestDouble.html\" title=\"TestDouble\" rel=\"nofollow noopener\" target=\"_blank\">TestDouble<\/a>\u53c3\u8003\u4f86\u7684\uff0c\u5982\u4e0b\u8868\u6240\u793a\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u540d\u8a5e<\/th>\n<th>\u8aaa\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>SUT<\/td>\n<td>System Under Test \u6211\u5011\u8981\u6e2c\u8a66\u7684\u5c0d\u8c61<\/td>\n<\/tr>\n<tr>\n<td>DOC<\/td>\n<td>Depended-On Component SUT\u9700\u8981\u7684\u5143\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>Dummy<\/td>\n<td>\u53ea\u662f\u7528\u4f86\u586b\u5145\u65b9\u6cd5\u7684\u53c3\u6578\uff0c\u5728\u65b9\u6cd5\u5167\u4e0d\u6703\u5be6\u969b\u88ab\u7528\u5230<\/td>\n<\/tr>\n<tr>\n<td>Fake<\/td>\n<td>\u7c21\u5316\u904e\u7684\u5143\u4ef6\u63d0\u4f9b\u8cc7\u8a0a\u8b93\u6e2c\u8a66\u53ef\u4ee5\u6b63\u5e38\u7684\u9032\u884c<\/td>\n<\/tr>\n<tr>\n<td>Stubs<\/td>\n<td>\u63d0\u4f9bSUT\u8cc7\u8a0a\u4f86\u9a57\u8b49SUT\u662f\u5426\u6709\u6b63\u5e38\u904b\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>Spies<\/td>\n<td>\u63d0\u4f9bSUT\u8cc7\u8a0a\uff0c\u4e26\u4e14\u76e3\u63a7SUT\u7684\u884c\u70ba<\/td>\n<\/tr>\n<tr>\n<td>Mocks<\/td>\n<td>\u7528\u4f86\u5e6b\u52a9SUT\u6aa2\u67e5\u5176\u884c\u70ba\u662f\u5426\u88ab\u57f7\u884c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u5c0f\u5149\u9047\u5230\u7684\u554f\u984c\u53ef\u4ee5\u900f\u904e<code>Stubs<\/code>\u4f86\u89e3\u6c7a\u800c\u5927\u982d\u9700\u8981\u904e<code>Mocks<\/code>\u4f86\u9a57\u8b49SUT\u7684\u884c\u70ba\u3002<\/p>\n<h3>NSubstitute<\/h3>\n<p>\u89e3\u91cb\u5b8c\u540d\u8a5e\u5f8c\uff0c\u63a5\u4e0b\u4f86\u8ddf\u5927\u5bb6\u4ecb\u7d39\u5982\u4f55\u900f\u904e\u5de5\u5177<a href=\"https:\/\/nsubstitute.github.io\/\" title=\"NSubstitute\" rel=\"nofollow noopener\" target=\"_blank\">NSubstitute<\/a>\u4f86\u8655\u7406\u4ee5\u4e0a\u7684\u554f\u984c\uff0c\u9996\u5148\u9084\u662f\u5148\u5b89\u88dd\u5957\u4ef6\uff0c\u8acb\u5404\u4f4d\u8f38\u5165\u4ee5\u4e0b\u6307\u4ee4\u4f86\u5b89\u88dd\u3002<\/p>\n<pre><code class=\"language-bash\">dotnet add package NSubstitute<\/code><\/pre>\n<p>\u800c\u4f7f\u7528\u65b9\u6cd5\u4e3b\u8981\u662f\u900f\u904e<code>Substitute.For&lt;\u4ecb\u9762&gt;()<\/code>\u4f86\u7522\u751f\u5143\u4ef6\uff0c\u518d\u4f86\u900f\u904e<code>.Get(Arg.Any&lt;int&gt;()).Return(x =&gt; {})<\/code>\u7522\u751f\u6a21\u64ec\u7684\u65b9\u6cd5\u3002\u6240\u4ee5\u6574\u500b\u8a9e\u6cd5\u53ef\u4ee5\u770b\u4e00\u4e0b\u4e0b\u5217\u7bc4\u4f8b\u3002<\/p>\n<pre><code class=\"language-cs\">var logger = Substitute.For&lt;ILogger&lt;LoginService&gt;&gt;(); \/\/ Dummy\nvar repo = Substitute.For&lt;IUserRepo&gt;(); \nrepo.Get(Arg.Any&lt;int&gt;()).Return(x =&gt; { return UserInfo()} ); \/\/ Fake, Stubs, Spies, Mocks\n\nvar sut = new LoginService(repo, logger);<\/code><\/pre>\n<p>\u4e0a\u9762\u4f8b\u5b50\u7c21\u55ae\u7684\u8aaa\u660e\u5982\u679c\u5728<code>LoginService<\/code>\u6709\u9700\u8981\u7528\u5230\u5143\u4ef6\u6642\u53ef\u4ee5\u900f\u904eSubstitute\u4f86\u7522\u751f\u5143\u4ef6\uff0c\u4e26\u4e14\u53ef\u4ee5\u6a21\u64ec\u8a72\u4ecb\u9762\u7684\u65b9\u6cd5\u8b93\u6e2c\u8a66\u53ef\u4ee5\u7e7c\u7e8c\u9032\u884c\u4e0b\u53bb\uff0c\u53c8\u6216\u8005\u53ef\u4ee5\u85c9\u7531\u6a21\u64ec\u56de\u50b3\u7279\u5b9a\u503c\u4f86\u9a57\u8b49SUT\u7684\u904b\u4f5c\u662f\u5426\u6b63\u5e38\u3002<\/p>\n<h2>\u5f8c\u8a18<\/h2>\n<p>\u4eca\u5929\u85c9\u7531\u5927\u982d\u7684\u6848\u4f8b\u8ddf\u5927\u5bb6\u4ecb\u7d39\u5982\u679c\u9047\u5230\u6709\u76f8\u4f9d\u898b\u9762\u7684\u5143\u4ef6\u8a72\u5982\u4f55\u6e2c\u8a66\uff0c\u539f\u5247\u4e0a\u55ae\u5143\u6e2c\u662f\u8981\u5c08\u6ce8\u5728\u53d7\u6e2c\u7684\u5143\u4ef6\u4e0a\uff0c\u6240\u4ee5\u901a\u5e38\u5c31\u4ee5NSubstitute\u4f86\u8655\u7406\u76f8\u4f9d\u7684\u4ecb\u9762\uff0c\u7136\u5f8c\u4f9d\u7167\u6e2c\u8a66\u6848\u4f8b\u4f86\u8fd4\u9084\u53d7\u6e2c\u5143\u4ef6\u6240\u9700\u7684\u8cc7\u6599\u85c9\u6b64\u9a57\u8b49\u53d7\u6e2c\u5143\u4ef6\u7684\u6b63\u78ba\u6027\uff0c\u6b64\u5916\u5c0d\u65bc\u4e00\u4e9b\u6e2c\u8a66\u7684\u540d\u8a5e\u4ecb\u7d39\u53ef\u5927\u5bb6\u53ef\u4ee5\u53c3\u8003<a href=\"https:\/\/jchu.cc\/2018\/08\/16-test.html\" title=\"\u6e2c\u8a66\u4e2d\u5e38\u898b\u7684\u540d\u8a5e\uff1aStub, Dummy, Mock..\u7b49\u7b49\" rel=\"nofollow noopener\" target=\"_blank\">\u6e2c\u8a66\u4e2d\u5e38\u898b\u7684\u540d\u8a5e\uff1aStub, Dummy, Mock..\u7b49\u7b49<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u6a21\u64ec\u8cc7\u6599 \u5728\u4e0a\u4e00\u7bc7\u4ecb\u7d39\u5b8cBDD\u4ee5\u53ca\u5982\u4f55\u5728VSCode\u64b0\u5bebBDD\u7684\u6e2c\u8a66\u6848\u4f8b\u4ee5\u53caStep\u4e4b\u5f8c\uff0c\u76f8\u4fe1\u5927\u5bb6\u5728\u64b0\u5beb\u6e2c &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2021\/10\/12\/nsubstitute\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008D-03-\u7522\u751f\u6a21\u64ec\u8cc7\u6599 ? NSubstitute\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[90,92,148],"class_list":["post-2539","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2021ironman","tag-dotnetcore","tag-nsubstitute"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=2539"}],"version-history":[{"count":21,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2539\/revisions"}],"predecessor-version":[{"id":2923,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2539\/revisions\/2923"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=2539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=2539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=2539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}