{"id":7773,"date":"2019-12-01T19:43:43","date_gmt":"2019-12-01T10:43:43","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7773"},"modified":"2023-05-27T22:29:35","modified_gmt":"2023-05-27T13:29:35","slug":"%e3%80%90rspec%e3%80%91let%e3%81%ab%e3%82%88%e3%82%8b%e5%85%b1%e9%80%9a%e5%8c%96","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7773","title":{"rendered":"\u3010Rspec\u3011\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u51e6\u7406\u306e\u5171\u901a\u5316(before\u3001let\u3001shared_examples)"},"content":{"rendered":"<h2>before\u30d6\u30ed\u30c3\u30af\u3092\u4f7f\u3063\u305f\u5171\u901a\u5316<\/h2>\n<p>before\u30d6\u30ed\u30c3\u30af\u3092\u4f7f\u3046\u3053\u3068\u3067describe\u3084context\u30d6\u30ed\u30c3\u30af\u306e\u5185\u90e8\u3067\u5404\u30c6\u30b9\u30c8\u5b9f\u884c\u524d\u306b\u5171\u901a\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">describe [\u30c6\u30b9\u30c8\u5bfe\u8c61],type:[spec\u306e\u7a2e\u985e] do\r\n  context [\u72b6\u614b] do\r\n    before do\r\n      [\u4e8b\u524d\u6e96\u5099]\r\n    end\r\n \r\n    it [\u4ed5\u69d8] do\r\n      [\u671f\u5f85\u52d5\u4f5c]\r\n    end\r\n  end\r\nend<\/pre>\n<h3>before\u30d6\u30ed\u30c3\u30af\u306b\u3088\u308b\u5171\u901a\u5316\u306e\u30c7\u30e1\u30ea\u30c3\u30c8<\/h3>\n<ul>\n<li>before\u30d6\u30ed\u30c3\u30af\u3067\u306f\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3059\u308b\u305f\u3073\u306b\u6bce\u56de\u5b9f\u884c\u3055\u308c\u308b\u306e\u3067\u30c6\u30b9\u30c8\u306b\u4e88\u671f\u3057\u306a\u3044\u5f71\u97ff\u3092\u53ca\u307c\u3059\u53ef\u80fd\u6027\u304c\u3042\u308b\u3002<\/li>\n<li>\u8981\u4ef6\u304c\u5897\u3048\u308b\u306b\u9023\u308c\u3066\u30c6\u30b9\u30c8\u306e\u53ef\u8aad\u6027\u304c\u60aa\u304f\u306a\u308b\u3002<\/li>\n<\/ul>\n<h4>before\u30d6\u30ed\u30c3\u30af\u3067\u306f\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3059\u308b\u305f\u3073\u306b\u6bce\u56de\u5b9f\u884c\u3055\u308c\u308b\u306e\u3067\u30c6\u30b9\u30c8\u306b\u4e88\u671f\u3057\u306a\u3044\u5f71\u97ff\u3092\u53ca\u307c\u3059\u53ef\u80fd\u6027\u304c\u3042\u308b\u3002<\/h4>\n<p>\u5168\u3066\u306e\u30c6\u30b9\u30c8\u304c\u4f7f\u308f\u306a\u3044\u4f59\u8a08\u306a\u30c7\u30fc\u30bf\u3092\u6301\u3064\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u4e88\u671f\u3057\u306a\u3044\u526f\u4f5c\u7528\u3092\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>let\uff08\u5171\u901a\u51e6\u7406\u306e\u5909\u6570\u5316\uff09<\/h2>\n<p>Rspec\u306e\u5171\u901a\u5316\u306f\u57fa\u672c\u7684\u306bbefore\u53e5\u306b\u3066\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u306f\u601d\u3044\u307e\u3059\u304c\u3001<span style=\"color: #ff0000;\"><strong>\u5171\u901a\u5316\u3057\u305f\u3044\u5185\u5bb9\u304c\u5fc5\u305a\u3057\u3082\u56fa\u5b9a\u5024\u3058\u3083\u7121\u3044\u5834\u5408<\/strong><\/span><span style=\"color: #ff0000;\"><strong>\u306b\u4f7f\u3048\u308b\u306e\u304clet\u3067\u5909\u6570\u7684\u306b\u51e6\u7406\u3092\u5171\u901a\u5316\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u6a5f\u80fd<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002let\u306f\u9045\u5ef6\u8aad\u307f\u8fbc\u307f\uff08\u547c\u3070\u308c\u305f\u969b\u306b\u521d\u3081\u3066\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\uff09\u3067\u3042\u308a\u3001before\u30d6\u30ed\u30c3\u30af\u306e\u5916\u90e8\u3067\u547c\u3070\u308c\u308b\u306e\u3067\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u306b\u5fc5\u8981\u306a\u30c6\u30b9\u30c8\u69cb\u9020\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h3>let\u306e\u69cb\u6587<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">\blet(:\u5b9a\u7fa9\u540d) {\u5b9a\u7fa9\u306e\u5185\u5bb9\u3001\u3064\u307e\u308aRspec\u3067\u884c\u3046\u51e6\u7406}<\/pre>\n<p>\u5b9a\u7fa9\u540d\u3068\u8a18\u8ff0\u3057\u305f\u7b87\u6240\u304c\u5b9f\u969b\u306b<span style=\"color: #ff0000;\"><strong>Rspec\u306e\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u5185\u3067\u4f7f\u3048\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5909\u6570<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>\u4f8b<\/h4>\n<pre class=\"lang:default decode:true\">\blet(:user) {FactoryBot.create(:user)}<\/pre>\n<p>\u4f8b\u3048\u3070\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067user\u3068\u3044\u3046\u5909\u6570\u306b\u5bfe\u3057\u3066FactoryBot\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u5b9a\u7fa9\u3057\u305fDB\u306e\u767b\u9332\u60c5\u5831\u3092\u683c\u7d0d\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>let\u304c\u547c\u3070\u308c\u308b\u30bf\u30a4\u30df\u30f3\u30b0<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>let\u304c\u547c\u3070\u308c\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u306f\u30d6\u30ed\u30c3\u30af\u5185\u3067\u521d\u3081\u3066\u5909\u6570\u3068\u3057\u3066\u547c\u3070\u308c\u305f\u30bf\u30a4\u30df\u30f3\u30b0<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002\u306a\u306e\u3067\u3001<span style=\"color: #ff0000;\"><strong>before\u30d6\u30ed\u30c3\u30af\u3067\u5909\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u5834\u5408\u3068\u306f\u7570\u306a\u308a\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u3068\u3057\u306a\u304f\u3066\u3082\u5909\u6570\u3092\u53c2\u7167\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002\u4e00\u5ea6\u3082\u547c\u3070\u308c\u306a\u3044let\u306f\u5b9f\u884c\u3055\u308c\u308b\u3053\u3068\u306f\u7121\u3044\u306e\u3067\u6ce8\u610f\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001context\u30d6\u30ed\u30c3\u30af\u306e\u4e2d\u306b\u8a18\u8f09\u3057\u305flet\u306f\u5fc5\u305a\u547c\u3070\u308c\u307e\u3059\u304c\u3001describe\u76f4\u4e0b\u306b\u914d\u7f6e\u3057\u305flet\u306f\u5909\u6570\u3068\u3057\u3066\u5b9f\u969b\u306b\u5225\u306e\u30b3\u30fc\u30c9\u3067\u4f7f\u308f\u308c\u30eb\u3053\u3068\u304c\u306a\u3044\u9650\u308a\u306f\u5b9f\u884c\u3055\u308c\u306a\u3044\u3067\u3059\u3002<\/p>\n<h3>let\u306f\u4e0a\u66f8\u304d\u3067\u304d\u308b\u3002<\/h3>\n<p>let\u3067\u5b9a\u7fa9\u3057\u305f\u5909\u6570\u306f\u5165\u308c\u5b50\u306b\u306a\u3063\u3066\u3044\u308b\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u5834\u5408\u306f\u3088\u308a\u4e0b\u306e\u968e\u5c64\u3067\u5b9a\u7fa9\u3057\u305flet\u306b\u3066\u4e0a\u66f8\u304d\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u57fa\u672c\u7684\u306b\u306f\u3053\u3046\u3044\u3046\u5024\u304c\u826f\u3044\u304c\u3001\u6570\u5c11\u306a\u3044\u7279\u5b9a\u306e\u5834\u5408\u3060\u3051\u306f\u5909\u3048\u305f\u3044\u3068\u8a00\u3063\u305f\u5834\u5408\u306b\u306f\u4f7f\u3048\u308b\u6a5f\u80fd<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<h3>let!\u306b\u3064\u3044\u3066<\/h3>\n<p>let\u306f\u5909\u6570\u3068\u3057\u3066\u5225\u306e\u30b3\u30fc\u30c9\u3067\u547c\u3093\u3067\u3042\u3052\u306a\u3044\u3068\u4f7f\u308f\u308c\u306a\u3044\u3067\u3059\u304c\u3001let!\u3068\u3057\u3066\u5b9a\u7fa9\u3059\u308c\u3070<span style=\"color: #ff0000;\"><strong>before\u306e\u524d\u306b\u5f37\u5236\u7684\u306b\u5b9f\u884c\u3055\u305b\u308b\u3053\u3068\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002\u8981\u306f<span style=\"color: #ff0000;\"><strong>\u9045\u5ef6\u8aad\u307f\u8fbc\u307f\u3092\u5f37\u5236\u7684\u306b\u89e3\u9664\u3057\u3066\u5909\u6570\u3092\u8aad\u307f\u8fbc\u307e\u305b\u308b\u3053\u3068\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002\u3042\u307e\u308a\u4f7f\u3046\u6a5f\u4f1a\u304c\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u65e2\u5b58\u306e\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u554f\u984c\u3067\u4f7f\u308f\u3056\u308b\u8ffd\u3048\u306a\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u306e\u3067\u77e5\u3063\u3066\u304a\u304f\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h4>let!\u306e\u7528\u9014<\/h4>\n<p>let\u3067\u5b9a\u7fa9\u3057\u305f\u5909\u6570\u306f\u305d\u306e\u30d6\u30ed\u30c3\u30af\u5185\u30671\u5ea6\u3067\u3082\u4f7f\u308f\u306a\u3044\u9650\u308a\u306f\u521d\u671f\u5316\u3055\u308c\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u306a\u306e\u3067\u3001<span style=\"color: #ff0000;\"><strong>\u5909\u6570\u306e\u5024\u3068\u3057\u3066\u306f\u4f7f\u308f\u306a\u3044\u304c\u5909\u6570\u306e\u4ef6\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u305f\u3044\u5834\u5408<\/strong><\/span>\u3068\u304b\u306e\u7528\u9014\u3067\u306f\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3001before\u30d6\u30ed\u30c3\u30af\u3068\u6a5f\u80fd\u306f\u540c\u3058\u3067\u3059\u3057\u300c!\u300d\u306f\u5c0f\u3055\u306a\u5909\u5316\u306e\u305f\u3081\u8aad\u307f\u9593\u9055\u3048\u3066\u3057\u307e\u3044\u3084\u3059\u3044\u306e\u3067\u3082\u3057\u4f7f\u3046\u5834\u5408\u306fbefore\u30d6\u30ed\u30c3\u30af\u3092\u4f7f\u3046\u3053\u3068\u3082\u691c\u8a0e\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>shared_context(context\u306e\u5171\u901a\u5316\uff09<\/h2>\n<p><span style=\"color: #ff0000;\"><strong>shared_context\u3092\u4f7f\u3046\u3068\u300c\u8907\u6570\u306e\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u306e\u51e6\u7406\u300d\u306e\u5171\u901a\u5316\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/strong><\/span><\/p>\n<h3>\u5171\u901a\u5316<\/h3>\n<pre class=\"lang:default decode:true\">RSpec.shared_context \"\u5171\u901a\u5316\u540d\" do\r\n\u3000\u51e6\u7406\u5185\u5bb9\r\nend<\/pre>\n<h3>\u547c\u3073\u51fa\u3057\u5074<\/h3>\n<pre class=\"lang:default decode:true \">include_context \"project setup\"<\/pre>\n<h2>shared_example(it\u306e\u5171\u901a\u5316\uff09<\/h2>\n<p>shared_example\u3092\u4f7f\u3046\u3068\u300c<span style=\"color: #ff0000;\"><strong>it\u5185\u306e\u51e6\u7406\u300d\u306e\u91cd\u8907\u3092\u5171\u901a\u5316\u3059\u308b\u6a5f\u80fd<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<h3>\u5171\u901a\u5316\u5f8c\u306e\u66f8\u304d\u65b9<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306bit\u306e\u51e6\u7406\u3092\u5171\u901a\u5316\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">shared_examples_for '\u5171\u901a\u51e6\u7406\u540d' do\r\n  it { \u5171\u901a\u51e6\u7406\u5185\u5bb9 }\r\nend<\/pre>\n<h3>\u547c\u3073\u51fa\u3057\u65b9<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u547c\u3073\u51fa\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">it_behaves_like '\u5171\u901a\u51e6\u7406\u540d'<\/pre>\n","protected":false},"excerpt":{"rendered":"before\u30d6\u30ed\u30c3\u30af\u3092\u4f7f\u3063\u305f\u5171\u901a\u5316 before\u30d6\u30ed\u30c3\u30af\u3092\u4f7f\u3046\u3053\u3068\u3067describe\u3084context\u30d6\u30ed\u30c3\u30af\u306e\u5185\u90e8\u3067\u5404\u30c6\u30b9\u30c8\u5b9f\u884c\u524d\u306b\u5171\u901a\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3067\u304d\u307e\u3059\u3002 describe [\u30c6\u30b9\u30c8\u5bfe\u8c61],typ [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[82,48,36],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7773"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7773"}],"version-history":[{"count":20,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7773\/revisions"}],"predecessor-version":[{"id":20463,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7773\/revisions\/20463"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7773"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}