Skip to content

Commit

Permalink
RSpec - User Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
BaoKhanh99 committed Apr 4, 2022
1 parent 1f8ce66 commit 12e681e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :user do
name { Faker::Name.name }
email { Faker::Internet.email }
password { "123456" }
admin { false }
end
end
3 changes: 2 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down

0 comments on commit 12e681e

Please sign in to comment.