{"id":7756,"date":"2019-12-01T15:49:50","date_gmt":"2019-12-01T06:49:50","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7756"},"modified":"2023-05-27T22:30:05","modified_gmt":"2023-05-27T13:30:05","slug":"%e3%80%90rspec%e3%80%91factorybot%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e6%ba%96%e5%82%99%e3%81%99%e3%82%8b%e3%80%82","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7756","title":{"rendered":"\u3010Rspec\u3011FactoryBot\u3067\u30c7\u30fc\u30bf\u3092\u6e96\u5099\u3059\u308b\u3002"},"content":{"rendered":"<p>Rails\u306b\u306f\u672c\u756a\u3001\u958b\u767a\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u306e3\u3064\u306e\u74b0\u5883\u304c\u3042\u308a\u307e\u3059\u304c\u30c6\u30b9\u30c8\u3092\u3059\u308b\u5834\u5408\u306f\u30c6\u30b9\u30c8\u74b0\u5883\u306b\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u6d41\u308c<\/h2>\n<ol>\n<li>FactoryBot\u3067\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u300c\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u300d\u3092\u7528\u610f\u3059\u308b\u3002<\/li>\n<li>Rspec\u306ebefore\u3067FactoryBot\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u5229\u7528\u3057\u3066\u30c6\u30b9\u30c8DB\u306b\u30c7\u30fc\u30bf\u3092\u633f\u5165\u3059\u308b\u3002<\/li>\n<\/ol>\n<h2>FactoryBot\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u7528\u610f<\/h2>\n<p>FactoryBot\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306f\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u751f\u6210\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">rails g factory_bot:model \u30e2\u30c7\u30eb\u540d<\/pre>\n<p>Rails\u306e\u300crspec\u300d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u300cfactories\u300d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u6210\u3057\u3066\u30e2\u30c7\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u4e0b\u8a18\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092<span style=\"color: #ff0000;\"><strong>\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3057\u305f\u3044\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3054\u3068\u306b\u7528\u610f\u3057\u307e\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">FactoryBot.define do\r\n  factory :\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u540d do\r\n    \u30ab\u30e9\u30e01 { '\u30ab\u30e9\u30e0\u5185\u5bb91' }\r\n    \u30ab\u30e9\u30e02 { '\u30ab\u30e9\u30e0\u5185\u5bb92' }\r\n\u3000\u3000 \u95a2\u9023\u3059\u308b\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u540d(\u95a2\u9023\u3055\u305b\u305f\u3044\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u304c\u3042\u308b\u5834\u5408\uff09\r\n  end\r\nend<\/pre>\n<p>\u300c:\u30e2\u30c7\u30eb\u540d\u300d\u3067\u6307\u5b9a\u3059\u308b\u30e2\u30c7\u30eb\u540d\u304b\u3089\u5b9f\u969b\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u540d\u3092\u81ea\u52d5\u3067\u985e\u63a8\u306f\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h3>\u95a2\u9023\u3059\u308b\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u540d<\/h3>\n<p>\u3082\u3057\u3001\u95a2\u9023\u3055\u305b\u305f\u3044\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u304c\u3042\u308b\u5834\u5408\u306ffactory\u30d6\u30ed\u30c3\u30af\u306e\u6700\u7d42\u884c\u306b\u8a18\u8ff0\u3092\u3057\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u306a\u304a\u30011:\u591a\u306e\u95a2\u4fc2\u3067\u3042\u308c\u3070\u591a\u5074\u306e\u30e2\u30c7\u30eb\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u307f\u306b\u8a18\u8ff0\u3059\u308b\u306e\u3067\u6ce8\u610f\u3067\u3059\u3002<\/strong><\/span><\/p>\n<h2>Rspec\u306ebefore\u3067FactoryBot\u3092\u7528\u3044\u3066\u30c7\u30fc\u30bf\u3092\u6e96\u5099\u3059\u308b\u3002<\/h2>\n<p>\u5b9f\u969b\u306b\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u969b\u306b\u4f7f\u3048\u308b\u30e1\u30bd\u30c3\u30c9\u306f\u4e0b\u8a18\u3067\u3059\u3002<\/p>\n<h3>\u65b0\u898f\u306b\u30c7\u30fc\u30bf\u3092\u767b\u9332\u3059\u308b\u3002<\/h3>\n<pre class=\"lang:default decode:true\">FactoryBot.create(:FactoryBot\u3067\u7528\u610f\u3057\u305f\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u540d)<\/pre>\n<p>\u305f\u3060\u3001\u3053\u306e\u307e\u307e\u3060\u3068\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u7528\u610f\u3057\u305f\u30c7\u30fc\u30bf\u3060\u3051\u304c\u305d\u306e\u307e\u307e\u4f7f\u3048\u308b\u3060\u3051\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u5185\u3067\u65b0\u3057\u304f\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3057\u305f\u3044\u5834\u5408\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">FactoryBot.create(:FactoryBot\u3067\u7528\u610f\u3057\u305f\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u540d,\u30ab\u30e9\u30e0\u540d1:'\u30ab\u30e9\u30e0\u5185\u5bb91',\u30ab\u30e9\u30e0\u540d2:'\u30ab\u30e9\u30e0\u5185\u5bb92')<\/pre>\n<p>\u307e\u305f\u30c6\u30fc\u30d6\u30eb\u540c\u58eb\u3092\u7d10\u4ed8\u3051\u3066\u4f5c\u6210\u3057\u305f\u3044\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308b\u5834\u5408\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">FactoryBot.create(:FactoryBot\u3067\u7528\u610f\u3057\u305f\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u540d,\u7d10\u4ed8\u3051\u305f\u3044\u30c6\u30fc\u30d6\u30eb\u540d:\u7d10\u4ed8\u3051\u305f\u3044\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3057\u305f\u30ed\u30fc\u30ab\u30eb\u5909\u6570)<\/pre>\n<p>\u4e8b\u524d\u306b<span style=\"color: #ff0000;\"><strong>\u7d10\u4ed8\u3051\u305f\u3044\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u540c\u3058\u304fFactoryBot.create\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308a\u4f5c\u6210\u3057\u3066\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u306b\u683c\u7d0d\u3057\u3066\u304a\u304d\u6307\u5b9a\u3057\u307e\u3059\u3002<\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"Rails\u306b\u306f\u672c\u756a\u3001\u958b\u767a\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u306e3\u3064\u306e\u74b0\u5883\u304c\u3042\u308a\u307e\u3059\u304c\u30c6\u30b9\u30c8\u3092\u3059\u308b\u5834\u5408\u306f\u30c6\u30b9\u30c8\u74b0\u5883\u306b\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 \u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u6d41\u308c FactoryBot\u3067\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u300c\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u300d\u3092\u7528\u610f\u3059 [&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\/7756"}],"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=7756"}],"version-history":[{"count":13,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7756\/revisions"}],"predecessor-version":[{"id":20465,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7756\/revisions\/20465"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7756"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}