{"id":8919,"date":"2019-12-30T08:50:56","date_gmt":"2019-12-29T23:50:56","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=8919"},"modified":"2023-05-27T22:29:01","modified_gmt":"2023-05-27T13:29:01","slug":"%e3%80%90rspec%e3%80%91%e3%80%8ccontroller-spec%e3%80%8d%e3%81%ae%e5%9f%ba%e6%9c%ac","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=8919","title":{"rendered":"\u3010Rspec\u3011\u300cController Spec(\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30b9\u30da\u30c3\u30af)\u300d\u306e\u57fa\u672c"},"content":{"rendered":"<h2>Controller Spec\u306f\u4f55\u3092\u30c6\u30b9\u30c8\u3059\u308b\u306e\u304b\u3002<\/h2>\n<p>HTTP\u30e1\u30bd\u30c3\u30c9\u3067\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u969b\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u306e\u6319\u52d5\u306e\u30c6\u30b9\u30c8\u3092\u3057\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30c6\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u3069\u3046\u3044\u3063\u305f\u30ec\u30b9\u30dd\u30f3\u30b9\u30b3\u30fc\u30c9\u304c\u8fd4\u3063\u3066\u304f\u308b\u306e\u304b\u3002<\/li>\n<li>\u3069\u3046\u3044\u3063\u305f\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3059\u308b\u306e\u304b\u3002<\/li>\n<li>\u3069\u3046\u3044\u3063\u305f\u5909\u6570\u304c\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u6e21\u3055\u308c\u308b\u3079\u304d\u306a\u306e\u304b\u3002<\/li>\n<\/ul>\n<h2>\u5b9f\u969b\u306b\u5b9f\u52d9\u3067\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30b9\u30da\u30c3\u30af\u3092\u5229\u7528\u3059\u308b\u5834\u5408<\/h2>\n<p>\u305d\u3082\u305d\u3082\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30b9\u30da\u30c3\u30af\u306f\u975e\u63a8\u5968\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u3042\u3048\u3066\u4f5c\u6210\u3059\u308b\u5834\u5408\u306f\u80a5\u5927\u5316\u3057\u3084\u3059\u3044\u306e\u3067\u4e0b\u8a18\u306e\u5185\u5bb9\u306b\u7d5e\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<ul>\n<li>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306e\u30a2\u30af\u30bb\u30b9\u5236\u5fa1\u304c\u6b63\u3057\u304f\u6a5f\u80fd\u3057\u3066\u3044\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3053\u3068<\/li>\n<\/ul>\n<p>\u5177\u4f53\u7684\u306b\u306f\u3001<span style=\"color: #ff0000;\"><strong>\u300c\u8a8d\u53ef\u3055\u308c\u3066\u3044\u306a\u3044\u30e6\u30fc\u30b6\u30fc\u300d\u3068\u300c\u30b2\u30b9\u30c8\u306b\u5bfe\u3059\u308b\u30e6\u30fc\u30b6\u30fc\u300d<\/strong><\/span>\u306e\u30c1\u30a7\u30c3\u30af\u3092\u3059\u308b\u30c6\u30b9\u30c8\u306e\u3053\u3068\u3067\u3059\u3002\u3088\u308a\u7d30\u304b\u3044\u30c6\u30b9\u30c8\u306b\u95a2\u3057\u3066\u306f\u3088\u308a\u4e0a\u4f4d\u306e\u30ec\u30d9\u30eb\u306e\u30c6\u30b9\u30c8\u3067\u88dc\u3046\u3088\u3046\u306b\u3059\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306e\u30c6\u30b9\u30c8\u3092\u3059\u308b\u306b\u3042\u305f\u308a\u4fbf\u5229\u306aGem<\/h2>\n<p><span style=\"color: #ff0000;\"><strong>\u300crails-controller-testing\u300d<\/strong><\/span>\u3068\u3044\u3046Gem\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">group :development, :test do\r\n  # Call 'byebug' anywhere in the code to stop execution and get a debugger console\r\n  gem 'byebug', platform: :mri\r\n  gem 'rails-flog', require: 'flog'\r\n  gem 'rspec-rails', '~&gt; 3.8'\r\n  gem 'rails-controller-testing' \u2605\u8ffd\u52a0\u3059\u308b\u3002\r\nend<\/pre>\n<p>bundle\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">bundle<\/pre>\n<p>Rails\u30b5\u30fc\u30d0\u306f\u518d\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<h2>Controller Spec\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u306e\u6e96\u5099<\/h2>\n<p>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306e\u30c6\u30b9\u30c8\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true\">rails g rspec:controller \u30e2\u30c7\u30eb\u540d<\/pre>\n<p>Docker\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30b3\u30de\u30f3\u30c9\u306b\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true\">docker-compose exec Rails\u306e\u30b3\u30f3\u30c6\u30ca\u540d bundle exec rails g rspec:controller \u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u540d<\/pre>\n<p class=\"p1\"><span class=\"s1\">\u300cspec\/controllers\/\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u540d_controller_spec.rb\u300d\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u304c\u751f\u6210\u3055\u308c\u308b\u306e\u3067\u3053\u308c\u304cController Spec\u306e\u30c6\u30b9\u30c8\u3092\u8a18\u8ff0\u3059\u308b\u305f\u3081\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u306a\u308a\u307e\u3059\u3002<\/span><\/p>\n<h2>\u30b5\u30f3\u30d7\u30eb<\/h2>\n<p>\u30e6\u30fc\u30b6\u30fc\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3092\u4f8b\u306b\u6319\u3052\u3066\u8aac\u660e\u3044\u305f\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">RSpec.describe UsersController, type: :controller do\r\n  describe '\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u5358\u4f4d\uff08\u4f8b\uff1aGET #index'\uff09 do\r\n    before { \r\n      get :index \r\n      @referer = 'http:\/\/localhost'\r\n      @request.env['HTTP_REFERER'] = @referer\r\n    }\r\n\r\n    #\u753b\u9762\u304b\u3089\u6e21\u3063\u3066\u304f\u308b\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u5b9a\u7fa9\r\n    let(:params) do\r\n      { user: {\r\n          name: 'user',\r\n          password: 'password',\r\n        }\r\n      }\r\n    end\r\n\r\n    it '\u30ec\u30b9\u30dd\u30f3\u30b9\u30b3\u30fc\u30c9\u304c200\u3067\u3042\u308b\u3053\u3068' do\r\n      expect(response).to have_http_status(:ok)\r\n    end\r\n\r\n    it 'new\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3059\u308b\u3053\u3068' do\r\n      expect(response).to render_template :new\r\n    end\r\n\r\n    it '\u65b0\u3057\u3044User\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u30d3\u30e5\u30fc\u306b\u6e21\u3055\u308c\u308b\u3053\u3068' do\r\n      expect(assigns(:user)).to be_a_new User\r\n    end\r\n  end\r\nend<\/pre>\n<h3>before { get : index }<\/h3>\n<p>UsersController\u306eindex\u30a2\u30af\u30b7\u30e7\u30f3\u306b\u7591\u4f3c\u7684\u306bget\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308aresponse\u3068\u3044\u3046\u5909\u6570\u306bGET\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u7d50\u679c\u304c\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002<\/p>\n<h4>get :\u30ea\u30af\u30a8\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9, format: :json(\u7701\u7565\u53ef), params: { \u5c5e\u60271:\u5c5e\u6027\u5024 }<\/h4>\n<p>\u300cformat:: json\u300d\u3092\u6307\u5b9a\u3059\u308b\u3068\u30ea\u30af\u30a8\u30b9\u30c8\u3092json\u5f62\u5f0f\u3067\u9001\u4fe1\u3057\u307e\u3059\u3002\uff08html\u5f62\u5f0f\u3067\u3082\u53ef\u80fd\u3067\u3059\u3002\uff09get\u30e1\u30bd\u30c3\u30c9\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6e21\u3057\u305f\u3044\u5834\u5408\u306f\u4e0a\u8a18\u306e\u3088\u3046\u306b\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h4>post :\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u540d, format: :json(\u7701\u7565\u53ef), params: { \u5c5e\u60271:\u5c5e\u6027\u5024 }<\/h4>\n<p>post\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u306f\u300c\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u540d\u300d(create\u3084\u3001show\u7b49)\u3092\u5f15\u6570\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<div>\n<h3>@referer = 'http:\/\/localhost'\u3000\u3068\u3000@request.env['HTTP_REFERER'] = @referer<\/h3>\n<\/div>\n<p>\u30ea\u30d5\u30a1\u30e9\u30fc\u306bURL\u3092\u57cb\u3081\u8fbc\u307f\u307e\u3059\u3002\u300credirect_to :back\u300d\u3068\u8a18\u8ff0\u3057\u305f\u5834\u5408\u306f\u30e6\u30fc\u30b6\u30fc\u304b\u3089\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306b\u542b\u307e\u308c\u308bHTTP\u30ea\u30d5\u30a1\u30e9\u30fc\u306b\u5024\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u3068\u52d5\u304d\u307e\u305b\u3093\u3002\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3067\u306f\u30ea\u30d5\u30a1\u30e9\u30fc\u306f\u8a2d\u5b9a\u3055\u308c\u306a\u3044\u306e\u3067\u660e\u793a\u7684\u306b\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u5185\u3067\u8a2d\u5b9a\u3057\u3066\u3042\u3052\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>response<\/h3>\n<p>\u30d6\u30e9\u30a6\u30b6\u306b\u8fd4\u3059\u3079\u304d\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u5168\u30c7\u30fc\u30bf\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002\u3053\u306e\u4e2d\u306b\u306fHTTP\u30ec\u30b9\u30dd\u30f3\u30b9\u30b3\u30fc\u30c9\u3082\u542b\u307e\u308c\u307e\u3059\u3002<\/p>\n<h3>\u30e1\u30bd\u30c3\u30c9<\/h3>\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>render_template<\/h4>\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<h4>be_a_new<\/h4>\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","protected":false},"excerpt":{"rendered":"Controller Spec\u306f\u4f55\u3092\u30c6\u30b9\u30c8\u3059\u308b\u306e\u304b\u3002 HTTP\u30e1\u30bd\u30c3\u30c9\u3067\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u969b\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u306e\u6319\u52d5\u306e\u30c6\u30b9\u30c8\u3092\u3057\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30c6\u30b9\u30c8\u3067\u3059\u3002 \u3069\u3046\u3044\u3063\u305f\u30ec\u30b9\u30dd\u30f3\u30b9\u30b3\u30fc\u30c9\u304c\u8fd4\u3063\u3066\u304f\u308b\u306e\u304b\u3002 \u3069\u3046\u3044\u3063\u305f\u30c6\u30f3 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[82,36],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/8919"}],"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=8919"}],"version-history":[{"count":19,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/8919\/revisions"}],"predecessor-version":[{"id":20461,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/8919\/revisions\/20461"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8919"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}