{"id":5211,"date":"2025-04-06T21:12:15","date_gmt":"2025-04-06T13:12:15","guid":{"rendered":"http:\/\/xinblog.ltd\/?p=5211"},"modified":"2025-04-07T10:04:01","modified_gmt":"2025-04-07T02:04:01","slug":"%e5%a6%82%e4%bd%95%e5%86%99%e6%b5%8b%e8%af%95-%e5%8d%81","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=5211","title":{"rendered":"\u5982\u4f55\u5199\u6d4b\u8bd5 \u5341"},"content":{"rendered":"<p>\u5982\u4f55\u5199\u6d4b\u8bd5 \u5341<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u8bf4\u4e0bTDD\u548cBDD\u4e24\u79cd\u6d4b\u8bd5\u5b9e\u8df5\u3002\u9996\u5148\u662fTDD\uff0c\u6d4b\u8bd5\u9a71\u52a8\u5f00\u53d1<\/p>\n<p>TDD\u5bf9\u4e8e\u5f88\u591a\u6765\u8bf4\uff0c\u4f1a\u7b80\u5316\u7684\u7406\u89e3\u4e3a\u5148\u5199\u6d4b\u8bd5\uff0c\u5728\u5199\u4ee3\u7801\u3002\u4f46\u662f\u8fd9\u53ea\u662f\u6d4b\u8bd5\u5148\u884c\u5f00\u53d1\uff0c\u800c\u4e0d\u662f\u6d4b\u8bd5\u9a71\u52a8\u5f00\u53d1\u3002<\/p>\n<p>\u90a3\u4e48\u4ec0\u4e48\u6837\u662f\u6d4b\u8bd5\u9a71\u52a8\u5f00\u53d1\u5462\uff1f<\/p>\n<p>\u5bf9\u4e8eTDD\u6765\u8bf4 \u5176\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u5706<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"535\" height=\"472\" class=\"wp-image-5212\" src=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/07\/word-image-4.png\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/07\/word-image-4.png 535w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/07\/word-image-4-300x265.png 300w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/p>\n<p>\u7ea2\u8272\u662f\u4e66\u5199\u4e86\u4e00\u4e2a\u65b0\u7684\u6d4b\u8bd5\uff0c\u7eff\u8272\u662f\u5199\u4e86\u529f\u80fd\u6d4b\u8bd5\uff0c\u91cd\u6784\u5c31\u662f\u5b8c\u6210\u4e4b\u540e\uff0c\u518d\u5ea6\u8c03\u6574\u4ee3\u7801\u3002<\/p>\n<p>\u5176\u548c\u6d4b\u8bd5\u5148\u884c\u5f00\u53d1\u7684\u533a\u522b\u5728\u4e8e\uff0c\u6d4b\u8bd5\u5148\u884c\u5f00\u53d1\u53ea\u662f\u5148\u5199\u6d4b\u8bd5\uff0c\u5728\u5b8c\u6210\u4ee3\u7801\u529f\u80fd\u3002\u5e76\u6ca1\u6709\u5f15\u5165\u91cd\u6784\u7684\u9636\u6bb5\u3002<\/p>\n<p>\u5982\u679c\u53ea\u662f\u5148\u5199\u6d4b\u8bd5\uff0c\u5728\u5199\u4f5c\u4e1a\u4ee3\u7801\u7684\u8bdd\uff0c\u4f1a\u51fa\u73b0\u56e0\u4e3a\u65b0\u589e\u4ee3\u7801\u800c\u5bfc\u81f4\u7684\u574f\u5473\u9053\u3002\u56e0\u4e3a\u6211\u4eec\u518d\u5b9a\u4e86\u6d4b\u8bd5\u4e4b\u540e\uff0c\u5f80\u5f80\u5728\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e3a\u4e86\u7b26\u5408\u6d4b\u8bd5\u800c\u8c03\u6574\u7684\u574f\u4ee3\u7801\u3002<\/p>\n<p>\u6240\u4ee5\u5728\u8fd9\u91cc\u5f15\u5165\u4e86\u91cd\u6784\uff0c\u5728\u91cd\u6784\u4e4b\u4e2d\uff0c\u4f1a\u5c06\u5473\u9053\u4e0d\u5bf9\u7684\u4ee3\u7801\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6d4b\u8bd5\u548c\u91cd\u6784\u76f8\u8f85\u76f8\u6210\uff0c\u4e00\u65e6\u6709\u4e86\u6d4b\u8bd5\uff0c\u5c31\u53ef\u4ee5\u5927\u80c6\u7684\u8fdb\u884c\u91cd\u6784\u4e86\uff0c\u91cd\u6784\u4e2d\u4e5f\u80fd\u4fdd\u8bc1\u540e\u7eed\u5f00\u53d1\u4e2d\u6d4b\u8bd5\u4ee3\u7801\u7684\u4e66\u5199\u96be\u6613\u3002<\/p>\n<p>\u518d\u4e4b\u540e\u5c31\u662fTDD\u7684\u5b9e\u8df5<\/p>\n<p>\u8fd9\u91cc\u7ed9\u51fa\u7684\u65b9\u6848\u662f\u8fdb\u884c\u5408\u7406\u7684\u4efb\u52a1\u5206\u89e3\u3002<\/p>\n<p>\u5982\u679c\u662f\u4e00\u4e2a\u4efb\u52a1\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u9700\u6c42\u5206\u7ea7\u4e3a\u4e00\u4e2a\u4e2a\u5b50\u4efb\u52a1\uff0c\u7136\u540e\u9a71\u52a8\u7740\u6211\u4eec\u7ed9\u51fa\u4e00\u4e2a\u4e2a\u53ef\u4ee5\u6d4b\u8bd5\u7684\u8bbe\u8ba1\uff0c\u90a3\u4e48\u5177\u4f53\u7684\u5199\u4ee3\u7801\u9636\u6bb5\uff0c\u6211\u4eec\u4f1a\u5148\u7ed9\u51fa\u6d4b\u8bd5\u4ee3\u7801\uff0c\u7136\u540e\u7ed9\u51fa\u5b9e\u9645\u4e1a\u52a1\u4ee3\u7801\uff0c\u6700\u540e\u91cd\u6784\u4fee\u6539\u5199\u51fa\u6765\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u6700\u540e\u8865\u5145\u4e00\u70b9\uff0c\u5982\u679c\u8981\u4f7f\u7528TDD\u5f00\u53d1\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u4ece\u6d4b\u8bd5\u7684\u89c6\u89d2\u6765\u770b\u5f85\u4ee3\u7801\u3002<\/p>\n<p>\u9664\u6b64\u5916\u5c31\u662f\u8ba8\u8bba\u4e0bBDD\u8fd9\u4e2a\u6d4b\u8bd5\u6846\u67b6\u3002<\/p>\n<p>BDD\u7684\u5168\u79f0\u662f\u884c\u4e3a\u9a71\u52a8\u5f00\u53d1\u3002<\/p>\n<p>\u8fd9\u4e2a\u6846\u67b6\u7684\u6d4b\u8bd5\u7684\u65b9\u5411\u662f\u9762\u5411\u4e1a\u52a1\u3002\u7531\u6d4b\u8bd5\u6846\u67b6\u53bb\u6a21\u62df\u7528\u6237\u7684\u884c\u4e3a\u3002\u4ece\u800c\u53eb\u505a\u884c\u4e3a\u9a71\u52a8\u5f00\u53d1\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u5c31\u770b\u4e0b\u4f7f\u7528\u6d41\u884c\u7684BDD\u6846\u67b6\uff0c\u8be5\u5982\u4f55\u8fdb\u884c\u5f00\u53d1\uff0c\u8fd9\u91cc\u4ee5Cucumber\u4e3a\u4f8b\u3002<\/p>\n<p>\u5176\u662f\u4e00\u4e2a\u652f\u6301\u4e0d\u540c\u8bed\u8a00\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u652f\u6301\u7684\u8bed\u8a00\u6709\u5e38\u89c1\u7684Java\uff0cJavascript\uff0cPHP\u7b49\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u770b\u4e0bBDD\u7684\u793a\u4f8b\uff0c\u5176\u573a\u666f\u5c31\u662f\u6211\u4eec\u5e38\u89c1\u7684\u5185\u5bb9\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>Scenario: List todo item<\/p>\n<p>Given todo item &#8220;foo&#8221; is added<\/p>\n<p>And todo item &#8220;bar&#8221; is added<\/p>\n<p>When list todo items<\/p>\n<p>Then todo item &#8220;foo&#8221; should be contained<\/p>\n<p>And todo item &#8220;bar&#8221; should be contained<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0cBDD\u5c55\u793a\u4e86\u4ed6\u4eec\u6846\u67b6\u5177\u6709\u5f88\u5f3a\u7684\u53ef\u8bfb\u6027\uff0c\u5176\u5229\u7528\u81ea\u8eab\u7684\u4e00\u79cd\u9886\u57df\u8bed\u8a00\u5b9e\u73b0\u4e86\u6d4b\u8bd5\u6d41\u7a0b\u7684\u7f16\u5199\u3002<\/p>\n<p>\u6838\u5fc3\u63cf\u8ff0\u5c31\u662fGiven When Then \u8fd9\u4e09\u4e2a\u7279\u5b9a\u63cf\u8ff0<\/p>\n<p>\u5176\u4e2dGiven\u662f\u524d\u63d0\u5047\u8bbe\uff0cWhen\u662f\u5177\u4f53\u64cd\u4f5c\uff0cThen\u5219\u662f\u8981\u9a8c\u8bc1\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u5bf9\u5e94\u7684\u5c31\u662f\u51c6\u5907\uff0c\u6267\u884c\uff0c \u65ad\u8a00\uff0c\u90a3\u4e48\u53bb\u6389\u8fd9\u4e9b\u7279\u5b9a\u63cf\u8ff0\uff0c\u5269\u4e0b\u7684\u901a\u7528\u63cf\u8ff0\u624d\u662f\u6211\u4eec\u8be5\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u5c31\u662f\u8981\u5b9e\u73b0\u4e00\u4e2a\u80f6\u6c34\u5c42\uff0c\u6216\u8005\u8bf4\u6b65\u9aa4\u5b9a\u4e49\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>public class TodoItemStepDefinitions &#8230; {<\/p>\n<p>private RestTemplate restTemplate;<\/p>\n<p>public TodoItemStepDefinitions() {<\/p>\n<p>&#8230;<\/p>\n<p>Given(&#8220;todo item {string} is added&#8221;, (String content) -&gt;<\/p>\n<p>addTodoItem(content)<\/p>\n<p>);<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>private void addTodoItem(final String content) {<\/p>\n<p>AddTodoItemRequest request = new AddTodoItemRequest(content);<\/p>\n<p>final ResponseEntity&lt;String&gt; entity =<\/p>\n<p>restTemplate.postForEntity(&#8220;http:\/\/localhost:8080\/todo-items&#8221;, request, String.class);<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0a\u9762\u6211\u4eec\u5229\u7528\u51fd\u6570\uff0c\u6765\u5b8c\u6210\u4e86\u5e94\u7528\u548cBDD\u7279\u5b9a\u8bed\u8a00\u63cf\u8ff0\u6587\u4ef6\u7684\u7275\u7ebf\u642d\u6865\u3002<\/p>\n<p>\u9664\u6b64\u5916\u9700\u8981\u6ce8\u610f\u5c31\u662f\u5728then\u7684\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u5229\u7528\u5229\u7528\u65ad\u8a00\u6765\u8bf4\u660e\u662f\u5426\u8fbe\u6210\u76ee\u7684\u3002<\/p>\n<p>\u9664\u6b64\u5916\u5c31\u662f\u4e00\u4e9b\u5bf9\u4e8eBDD\u6d4b\u8bd5\u7684\u773c\u795e\uff0c\u6bd4\u5982\u8fd8\u6709\u4e00\u4e9b\u7c7b\u4f3cBDD\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u3002\u6bd4\u5982RSpec\uff0c\u5176\u66f4\u52a0\u9002\u5408\u4e8e\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>RSpec.describe Order do<\/p>\n<p>it &#8220;sums the prices of its line items&#8221; do<\/p>\n<p>order = Order.new<\/p>\n<p>order.add_entry(LineItem.new(:item =&gt; Item.new(<\/p>\n<p>:price =&gt; Money.new(1.11, :USD)<\/p>\n<p>)))<\/p>\n<p>order.add_entry(LineItem.new(:item =&gt; Item.new(<\/p>\n<p>:price =&gt; Money.new(2.22, :USD),<\/p>\n<p>:quantity =&gt; 2<\/p>\n<p>)))<\/p>\n<p>expect(order.total).to eq(Money.new(5.55, :USD))<\/p>\n<p>end<\/p>\n<p>end<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd8\u6709\u5c31\u662f\u5bf9\u4e1a\u52a1\u9700\u6c42\u7684\u6587\u6863\u8bdd\u63cf\u8ff0\uff0c\u5176\u53ef\u4ee5\u5c06BDD\u76f8\u5173\u7684\u9886\u57df\u8bed\u8a00\u8f6c\u6362\u4e3a\u76f8\u5173\u6587\u6863\u3002\u5c31\u6bd4\u5982Cucumber\u5c31\u6709\u7740\u5bf9\u4e8e\u6d3b\u6587\u6863\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u548cJIRA\u8fdb\u884c\u96c6\u6210\uff0c\u5c06Cucumber\u6d4b\u8bd5\u53d8\u4e3a\u6587\u6863\u3002<\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8ba9\u6d4b\u8bd5\u66f4\u52a0\u8d34\u8fd1\u4e8e\u4e1a\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5199\u6d4b\u8bd5 \u5341 \u8fd9\u91cc\u6211\u4eec\u8bf4\u4e0bT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5211"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5211"}],"version-history":[{"count":1,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5211\/revisions"}],"predecessor-version":[{"id":5213,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5211\/revisions\/5213"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5211"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}