From 43067c847fbaf7d822dc1a4521a208b60a4d9099 Mon Sep 17 00:00:00 2001 From: i544c Date: Tue, 17 Oct 2017 18:08:13 +0900 Subject: [PATCH] =?UTF-8?q?URL=E6=A7=8B=E6=88=90=E5=A4=89=E6=9B=B4?= =?UTF-8?q?=E5=BE=8C=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 1 + spec/controllers/meals_controller_spec.rb | 145 +----------------- .../user_profile_controller_spec.rb | 30 ---- spec/controllers/users_controller_spec.rb | 19 +++ spec/factories/user_profiles.rb | 2 +- spec/routing/meals_routing_spec.rb | 16 +- 6 files changed, 30 insertions(+), 183 deletions(-) delete mode 100644 spec/controllers/user_profile_controller_spec.rb create mode 100644 spec/controllers/users_controller_spec.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 65f001f..24e546a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,5 @@ class UsersController < ApplicationController + before_action :authenticate_user! before_action :set_user def show diff --git a/spec/controllers/meals_controller_spec.rb b/spec/controllers/meals_controller_spec.rb index bdef0c3..6ef7265 100644 --- a/spec/controllers/meals_controller_spec.rb +++ b/spec/controllers/meals_controller_spec.rb @@ -1,148 +1,5 @@ require "rails_helper" -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. -# -# Also compared to earlier versions of this generator, there are no longer any -# expectations of assigns and templates rendered. These features have been -# removed from Rails core in Rails 5, but can be added back in via the -# `rails-controller-testing` gem. - RSpec.describe MealsController, type: :controller do - let(:user) { FactoryGirl.build(:user) } - - # This should return the minimal set of attributes required to create a valid - # Meal. As you add validations to Meal, be sure to - # adjust the attributes here as well. - let(:valid_attributes) { - skip("Add a hash of attributes valid for your model") - } - - let(:invalid_attributes) { - skip("Add a hash of attributes invalid for your model") - } - - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # MealsController. Be sure to keep this updated too. - let(:valid_session) { {} } - - before do - login_user user - end - - after do - sign_out user - end - - describe "GET #index" do - it "returns a success response" do - get :index, params: {}, session: valid_session - expect(response).to be_success - end - end - - describe "GET #show" do - it "returns a success response" do - meal = Meal.create! valid_attributes - get :show, params: { id: meal.to_param }, session: valid_session - expect(response).to be_success - end - end - - describe "GET #new" do - it "returns a success response" do - get :new, params: {}, session: valid_session - expect(response).to be_success - end - end - - describe "GET #edit" do - it "returns a success response" do - meal = Meal.create! valid_attributes - get :edit, params: { id: meal.to_param }, session: valid_session - expect(response).to be_success - end - end - - describe "POST #create" do - context "with valid params" do - it "creates a new Meal" do - expect { - post :create, params: { meal: valid_attributes }, session: valid_session - }.to change(Meal, :count).by(1) - end - - it "redirects to the created meal" do - post :create, params: { meal: valid_attributes }, session: valid_session - expect(response).to redirect_to(Meal.last) - end - end - - context "with invalid params" do - it "returns a success response (i.e. to display the 'new' template)" do - post :create, params: { meal: invalid_attributes }, session: valid_session - expect(response).to be_success - end - end - end - - describe "PUT #update" do - context "with valid params" do - let(:new_attributes) { - skip("Add a hash of attributes valid for your model") - } - - it "updates the requested meal" do - meal = Meal.create! valid_attributes - put :update, params: { id: meal.to_param, meal: new_attributes }, session: valid_session - meal.reload - skip("Add assertions for updated state") - end - - it "redirects to the meal" do - meal = Meal.create! valid_attributes - put :update, params: { id: meal.to_param, meal: valid_attributes }, session: valid_session - expect(response).to redirect_to(meal) - end - end - - context "with invalid params" do - it "returns a success response (i.e. to display the 'edit' template)" do - meal = Meal.create! valid_attributes - put :update, params: { id: meal.to_param, meal: invalid_attributes }, session: valid_session - expect(response).to be_success - end - end - end - - describe "DELETE #destroy" do - it "destroys the requested meal" do - meal = Meal.create! valid_attributes - expect { - delete :destroy, params: { id: meal.to_param }, session: valid_session - }.to change(Meal, :count).by(-1) - end - - it "redirects to the meals list" do - meal = Meal.create! valid_attributes - delete :destroy, params: { id: meal.to_param }, session: valid_session - expect(response).to redirect_to(meals_url) - end - end + pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/controllers/user_profile_controller_spec.rb b/spec/controllers/user_profile_controller_spec.rb deleted file mode 100644 index e618d48..0000000 --- a/spec/controllers/user_profile_controller_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require "rails_helper" - -RSpec.describe UserProfileController, type: :controller do - let(:user) { FactoryGirl.build(:user) } - - describe "GET #show" do - subject { get :show, params: params } - - context "存在するnameでアクセスした時" do - let(:params) { { name: user.profile.nickname } } - it_behaves_like "ログイン画面に遷移する" - end - - context "存在しないnameでアクセスした時" do - let(:params) { { name: "sachiko" } } - it_behaves_like "ログイン画面に遷移する" - end - end - - describe "GET #edit" do - subject { get :edit, params: params } - - context "存在するnameでアクセスした時" do - let(:params) { { name: user.profile.nickname } } - it "ログイン画面に遷移する" do - is_expected.to redirect_to new_user_session_path - end - end - end -end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 0000000..983c9c4 --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -0,0 +1,19 @@ +require "rails_helper" + +RSpec.describe UsersController, type: :controller do + let(:user) { FactoryGirl.create(:user) } + + describe "GET #show" do + subject { get :show, params: params } + + context "存在するnameでアクセスした時" do + let(:params) { { name: user.username } } + it_behaves_like "ログイン画面に遷移する" + end + + context "存在しないnameでアクセスした時" do + let(:params) { { name: "@sachiko" } } + it_behaves_like "ログイン画面に遷移する" + end + end +end diff --git a/spec/factories/user_profiles.rb b/spec/factories/user_profiles.rb index c5222d0..43e6164 100644 --- a/spec/factories/user_profiles.rb +++ b/spec/factories/user_profiles.rb @@ -14,7 +14,7 @@ FactoryGirl.define do factory :user_profile do - sequence(:username) { |n| "koume-#{n}" } + sequence(:username) { |n| "koume#{n}" } nickname "zombie lover" avatar "zombieee" bio "I love zombie💓" diff --git a/spec/routing/meals_routing_spec.rb b/spec/routing/meals_routing_spec.rb index f824431..93ee591 100644 --- a/spec/routing/meals_routing_spec.rb +++ b/spec/routing/meals_routing_spec.rb @@ -3,35 +3,35 @@ RSpec.describe MealsController, type: :routing do describe "routing" do it "routes to #index" do - expect(get: "/meals").to route_to("meals#index") + expect(get: "/@koume1/meals").to route_to("meals#index", user_name: "@koume1") end it "routes to #new" do - expect(get: "/meals/new").to route_to("meals#new") + expect(get: "/@koume1/meals/new").to route_to("meals#new", user_name: "@koume1") end it "routes to #show" do - expect(get: "/meals/1").to route_to("meals#show", id: "1") + expect(get: "/@koume1/meals/2017/03/28").to route_to("meals#show", user_name: "@koume1", date: "2017/03/28") end it "routes to #edit" do - expect(get: "/meals/1/edit").to route_to("meals#edit", id: "1") + expect(get: "/@koume1/meals/2017/03/28/edit").to route_to("meals#edit", user_name: "@koume1", date: "2017/03/28") end it "routes to #create" do - expect(post: "/meals").to route_to("meals#create") + expect(post: "/@koume1/meals").to route_to("meals#create", user_name: "@koume1") end it "routes to #update via PUT" do - expect(put: "/meals/1").to route_to("meals#update", id: "1") + expect(put: "/@koume1/meals/2017/03/28").to route_to("meals#update", user_name: "@koume1", date: "2017/03/28") end it "routes to #update via PATCH" do - expect(patch: "/meals/1").to route_to("meals#update", id: "1") + expect(patch: "/@koume1/meals/2017/03/28").to route_to("meals#update", user_name: "@koume1", date: "2017/03/28") end it "routes to #destroy" do - expect(delete: "/meals/1").to route_to("meals#destroy", id: "1") + expect(delete: "/@koume1/meals/2017/03/28").to route_to("meals#destroy", user_name: "@koume1", date: "2017/03/28") end end end