diff --git a/Gemfile.lock b/Gemfile.lock index 12690f4..e89db8b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -183,6 +183,10 @@ GEM bundler (>= 1.3.0) railties (= 6.0.4.7) sprockets-rails (>= 2.0.0) + rails-controller-testing (1.0.5) + actionpack (>= 5.0.1.rc1) + actionview (>= 5.0.1.rc1) + activesupport (>= 5.0.1.rc1) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) @@ -315,6 +319,7 @@ DEPENDENCIES pagy puma (~> 4.1) rails (~> 6.0.4, >= 6.0.4.6) + rails-controller-testing rails-i18n rspec-rails (~> 4.0.1) rubocop (~> 0.74.0) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 0000000..a79e9bf --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -0,0 +1,25 @@ +require "rails_helper" +include SessionsHelper + +RSpec.describe UsersController, type: :controller do + describe "GET #show" do + let!(:user) { create(:user)} + + context "when user logged in" do + before do + log_in user + get :show, params: { id: user.id } + end + it "render show template" do + expect(response).to render_template(:show) + end + end + + context "when user not logged in" do + it "redirect to login template" do + get :show, params: { id: user.id } + expect(response).to redirect_to login_path + end + end + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 0000000..accf009 --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :user do + name { Faker::Name.name } + email { Faker::Internet.email } + password { "123456" } + admin { false } + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 2590810..208f628 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -4,7 +4,8 @@ abort("The Rails environment is running in production mode!") if Rails.env.production? require "rspec/rails" -require "support/factory_bot" + +Dir[Rails.root.join("spec", "support", "**", "*.rb")].each { |f| require f } begin ActiveRecord::Migration.maintain_test_schema!