{"id":7784,"date":"2019-12-01T20:22:24","date_gmt":"2019-12-01T11:22:24","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7784"},"modified":"2023-05-27T22:29:20","modified_gmt":"2023-05-27T13:29:20","slug":"%e3%80%90rspec%e3%80%91it%e3%81%ae%e3%83%9e%e3%83%83%e3%83%81%e3%83%a3%e3%81%ae%e7%a8%ae%e9%a1%9e","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7784","title":{"rendered":"\u3010Rspec\u3011it\u306e\u30de\u30c3\u30c1\u30e3\u306e\u7a2e\u985e\uff08\u30ab\u30b9\u30bf\u30e0\u30de\u30c3\u30c1\u30e3\u3001aggregate_failures\u306a\u3069\u3082\uff09"},"content":{"rendered":"<h2>System Spec\u306e\u5834\u5408<\/h2>\n<h3>have_selector<\/h3>\n<p>CSS\u30bb\u30ec\u30af\u30bf\u5185\u306b\u7279\u5b9a\u306e\u8981\u7d20\u3092\u542b\u3093\u3067\u3044\u308b\u304b\u691c\u67fb\u3057\u307e\u3059\u3002<\/p>\n<h4>\u4f8b<\/h4>\n<p>\u4e0b\u8a18\u306e\u4f8b\u3060\u3068.nav\u3068\u3044\u3046\u30af\u30e9\u30b9\u306b\u30bf\u30b9\u30af\u3068\u3044\u3046\u6587\u5b57\u5217\u3092\u30c6\u30ad\u30b9\u30c8\u306b\u542b\u3080\u3082\u306e\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3092\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">expect(page).to have_selector '.nav' text: '\u30bf\u30b9\u30af'<\/pre>\n<h3>have_content<\/h3>\n<p>\u30da\u30fc\u30b8\u5185\u306b\u7279\u5b9a\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<h3>have_no_content<\/h3>\n<p>\u30da\u30fc\u30b8\u5185\u306b\u7279\u5b9a\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u304c\u306a\u3044\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<h2>Model Spec\u306e\u5834\u5408<\/h2>\n<h3>eq<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>rspec-expectations<\/strong><\/span>\u304c\u63d0\u4f9b\u3059\u308b\u30de\u30c3\u30c1\u30e3\u3067\u30e1\u30bd\u30c3\u30c9\u306e\u7d50\u679c\u304c\u6307\u5b9a\u3057\u305f\u5024\u3068\u7b49\u3057\u3044\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002\u304b\u306a\u308a\u4f7f\u7528\u983b\u5ea6\u304c\u9ad8\u3044\u30de\u30c3\u30c1\u30e3\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>\u4f8b<\/h4>\n<pre class=\"lang:default decode:true\">expect(user.age).to eq 3<\/pre>\n<h3>be_valid<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>rspec-rails<\/strong><\/span>\u304c\u63d0\u4f9b\u3059\u308b\u30de\u30c3\u30c1\u30e3\u3067\u30e2\u30c7\u30eb\u304c\u6709\u52b9\u306a\u72b6\u614b\u3067\u3042\u308b\u304b\u3092\u5224\u5b9a\u3059\u308b\u3002<\/p>\n<h3>include<\/h3>\n<p>\u6587\u5b57\u5217\u304c\u542b\u307e\u308c\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3002<\/p>\n<h3>be_empty<\/h3>\n<p>\u8fd4\u5374\u3055\u308c\u305f\u914d\u5217\u304b\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u304c\u7a7a\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3002<\/p>\n<h3>be_late<\/h3>\n<p>Rspec\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u30de\u30c3\u30c1\u30e3\u3067\u306f\u306a\u3044\u3067\u3059\u304c\u3001\u4f8b\u3048\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e<span style=\"color: #ff0000;\"><strong>\u30e2\u30c7\u30eb\u5185\u306b\u300clate\u300d\u307e\u305f\u306f\u300clate?\u300d\u3068\u3044\u3046\u540d\u524d\u306e\u3064\u3044\u305f\u771f\u507d\u5024\u3092\u8fd4\u3059\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308c\u3070\u3053\u306e\u30de\u30c3\u30c1\u30e3\u3092\u4f7f\u3048\u3070\u547c\u3073\u51fa\u3057\u3066\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u5224\u5b9a\u306b\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">class \u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u540d &lt; ApplicationRecord\r\n  def late?\r\n\u3000\u3000\u3000\u3000\u5c5e\u6027\u306e\u6bd4\u8f03\u51e6\u7406\r\n  end\r\nend<\/pre>\n<h2>Controller Spec\u306e\u5834\u5408<\/h2>\n<h3>\u30b9\u30c6\u30fc\u30bf\u30b9\u30c1\u30a7\u30c3\u30af\u7cfb<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306bHTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u30de\u30c3\u30c1\u30e3\u306f\u8907\u6570\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u3069\u3061\u3089\u3092\u4f7f\u3046\u304b\u306fHTTP\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u8fd4\u3059\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u8907\u96d1\u3055\u306b\u3088\u3063\u3066\u6c7a\u3081\u308b\u3068\u826f\u3044<\/strong><\/span>\u3067\u3057\u3087\u3046\u3002\u8272\u3005\u306a\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<div>\n<h4>respond_with(:success)\uff1ashoulda-matchers\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u5fc5\u8981<\/h4>\n<p>\u5b9f\u52d9\u3067\u306f\u3053\u308c\u3092\u4f7f\u3063\u3066\u3044\u308b\u3053\u3068\u3082\u591a\u3044\u3067\u3059\u3002<\/p>\n<\/div>\n<h4>have_http_status \"\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9(\u4f8b\uff1a302\u7b49)\"<\/h4>\n<p>HTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9\u304c\u4e00\u81f4\u3057\u3066\u3044\u308b\u304b\u30c6\u30b9\u30c8\u3057\u307e\u3059\u3002<\/p>\n<h4>be_success<\/h4>\n<div>\n<p>\u30ec\u30b9\u30dd\u30f3\u30b9\u30b9\u30c6\u30fc\u30bf\u30b9\u304c\u6210\u529f(200)\u304b\u305d\u308c\u4ee5\u5916(500\u7b49)\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<\/div>\n<h3>render_template<\/h3>\n<p>\u6307\u5b9a\u3057\u305f\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304c\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u3066\u3044\u308b\u304b\u30c6\u30b9\u30c8\u3067\u304d\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u300crails-controller-testing\u300d<\/strong><\/span>\u3068\u3044\u3046Gem\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3053\u3068\u3067\u5229\u7528\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>be_a_new<\/h3>\n<p>\u65b0\u3057\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>change<\/h3>\n<p>DB\u306b\u30c7\u30fc\u30bf\u4ef6\u6570\u304c1\u4ef6\u5897\u3048\u3066\u3044\u308b\u3053\u3068\u3092\u30c6\u30b9\u30c8\u3067\u304d\u307e\u3059\u3002<\/p>\n<div>\n<h3>redirect_to \"\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3092\u671f\u5f85\u3059\u308bURL\"<\/h3>\n<\/div>\n<div>\u6307\u5b9a\u3057\u305fURL\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/div>\n<h3>include<\/h3>\n<div>\u914d\u5217\u306b\u7279\u5b9a\u306e\u6587\u5b57\u5217\u304c\u542b\u307e\u308c\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08flash\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u30c1\u30a7\u30c3\u30af\u3068\u304b\u306b\u4f7f\u3048\u307e\u3059\u3002\uff09<\/div>\n<div>\n<h2>\u30ab\u30b9\u30bf\u30e0\u30de\u30c3\u30c1\u30e3\u3068\u306f\uff1f<\/h2>\n<p>Rspec\u306e\u30de\u30c3\u30c1\u30e3\u306f\u81ea\u4f5c\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h3>\u4f5c\u6210\u65b9\u6cd5<\/h3>\n<p>\u4e0b\u8a18\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">spec\/support\/matchers\/\u30ab\u30b9\u30bf\u30e0\u30de\u30c3\u30c1\u30e3\u540d.rb<\/pre>\n<p>\u5185\u5bb9<\/p>\n<pre class=\"lang:default decode:true\">RSpec::Matchers.define :\u547c\u3073\u51fa\u3059\u540d\u524d \u30de\u30c3\u30c1\u30e3\u306e\u5185\u5bb9<\/pre>\n<h2>aggregate_failures\u3068\u306f\uff1f<\/h2>\n<p>\u4e00\u3064\u306eexpect\u304c\u5931\u6557\u3057\u305f\u969b\u306b\u901a\u5e38\u3067\u3042\u308c\u3070\u6b21\u306eit\u306b\u79fb\u52d5\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u304c\u3001aggregate_failures\u3092\u4f7f\u3046\u3053\u3068\u306b\u3088\u3063\u3066\u4eee\u306b\u5931\u6557\u3057\u305f\u3068\u3057\u3066\u3082\u6b21\u306eexpect\u3092\u5b9f\u884c\u3057\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>\u8a18\u8ff0\u65b9\u6cd5<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u300caggregate_failures\u300d\u3068\u3044\u3046\u69cb\u6587\u3067\u304f\u308b\u3080\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\"> # \u6b63\u5e38\u306b\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u3053\u3068\r\nit \"responds successfully\" do\r\n   get :index\r\n   aggregate_failures do\r\n      expect(response).to be_success\u3000\u2605\u3053\u3053\u3067\u5931\u6557\u3059\u308b\u3002\r\n      expect(response).to have_http_status \"200\"  \u2605\u672c\u6765\u306f\u3053\u306e\u884c\u306f\u5b9f\u884c\u3055\u308c\u306a\u3044\u304c\u5b9f\u884c\u3055\u308c\u308b\u3002\r\n   end\r\nend<\/pre>\n<h3>\u6ce8\u610f\u70b9<\/h3>\n<p>aggregate_failures\u306f\u5931\u6557\u3059\u308bexpect\u306b\u5bfe\u3057\u3066\u3060\u3051\u50cd\u304f\u3082\u306e\u3067\u3042\u308a\u4e00\u822c\u7684\u306a\u30a8\u30e9\u30fc\u306b\u306f\u50cd\u304b\u306a\u3044\u306e\u3067\u6ce8\u610f\u3067\u3059\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"System Spec\u306e\u5834\u5408 have_selector CSS\u30bb\u30ec\u30af\u30bf\u5185\u306b\u7279\u5b9a\u306e\u8981\u7d20\u3092\u542b\u3093\u3067\u3044\u308b\u304b\u691c\u67fb\u3057\u307e\u3059\u3002 \u4f8b \u4e0b\u8a18\u306e\u4f8b\u3060\u3068.nav\u3068\u3044\u3046\u30af\u30e9\u30b9\u306b\u30bf\u30b9\u30af\u3068\u3044\u3046\u6587\u5b57\u5217\u3092\u30c6\u30ad\u30b9\u30c8\u306b\u542b\u3080\u3082\u306e\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3092\u3057\u307e\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\/7784"}],"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=7784"}],"version-history":[{"count":18,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7784\/revisions"}],"predecessor-version":[{"id":20462,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7784\/revisions\/20462"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7784"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}