Skip to content

Commit

Permalink
add controllers test
Browse files Browse the repository at this point in the history
  • Loading branch information
loftwah committed Aug 23, 2024
1 parent 552977c commit c375a66
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem 'capybara'
gem 'selenium-webdriver'
gem 'rails-controller-testing'
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ GEM
activesupport (= 7.1.3.4)
bundler (>= 1.15.0)
railties (= 7.1.3.4)
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.2.0)
activesupport (>= 5.0.0)
minitest
Expand Down Expand Up @@ -392,6 +396,7 @@ DEPENDENCIES
mini_magick
puma (>= 5.0)
rails (~> 7.1.3, >= 7.1.3.4)
rails-controller-testing
redis (>= 4.0.1)
rspec-rails (~> 6.0.0)
selenium-webdriver
Expand Down
80 changes: 80 additions & 0 deletions spec/controllers/achievements_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# spec/controllers/achievements_controller_spec.rb
require 'rails_helper'

RSpec.describe AchievementsController, type: :controller do
let(:user) { create(:user) }
let(:achievement) { create(:achievement, user: user) }

describe "GET #index" do
it "returns a success response" do
get :index
expect(response).to be_successful
end
end

describe "GET #show" do
context "when achievement doesn't have a URL" do
it "returns a success response" do
get :show, params: { id: achievement.to_param }
expect(response).to be_successful
end
end

context "when achievement has a URL" do
let(:achievement_with_url) { create(:achievement, user: user, url: 'http://example.com') }

it "redirects to the achievement URL" do
get :show, params: { id: achievement_with_url.to_param }
expect(response).to redirect_to(achievement_with_url.url)
end
end

it "creates an AchievementView" do
expect {
get :show, params: { id: achievement.to_param }
}.to change(AchievementView, :count).by(1)
end
end

describe "GET #new" do
it "returns a success response" do
sign_in user
get :new
expect(response).to be_successful
end
end

describe "POST #create" do
context "with valid params" do
it "creates a new Achievement" do
sign_in user
expect {
post :create, params: { achievement: attributes_for(:achievement) }
}.to change(Achievement, :count).by(1)
end
end
end

describe "PUT #update" do
context "with valid params" do
let(:new_attributes) { { title: "New Title" } }

it "updates the requested achievement" do
sign_in user
put :update, params: { id: achievement.to_param, achievement: new_attributes }
achievement.reload
expect(achievement.title).to eq("New Title")
end
end
end

describe "DELETE #destroy" do
it "destroys the requested achievement" do
sign_in user
achievement # ensure achievement is created before the expect block
expect {
delete :destroy, params: { id: achievement.to_param }
}.to change(Achievement, :count).by(-1)
end
end
end
31 changes: 31 additions & 0 deletions spec/controllers/analytics_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# spec/controllers/analytics_controller_spec.rb
require 'rails_helper'

RSpec.describe AnalyticsController, type: :controller do
let(:user) { create(:user) }

before do
sign_in user
create(:daily_metric, user: user, date: Date.today)
end

describe "GET #index" do
it "returns a success response" do
get :index
expect(response).to be_successful
end

it "assigns the correct instance variables" do
get :index
expect(assigns(:total_page_views)).to be_a(Integer)
expect(assigns(:total_link_clicks)).to be_a(Integer)
expect(assigns(:total_achievement_views)).to be_a(Integer)
expect(assigns(:unique_visitors)).to be_a(Integer)
expect(assigns(:latest_daily_metric)).to be_a(DailyMetric)
expect(assigns(:link_analytics)).to be_an(Array)
expect(assigns(:achievement_analytics)).to be_an(Array)
expect(assigns(:daily_views)).to be_a(Hash)
expect(assigns(:browser_data)).to be_a(Hash)
end
end
end
91 changes: 91 additions & 0 deletions spec/controllers/links_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# spec/controllers/links_controller_spec.rb
require 'rails_helper'

RSpec.describe LinksController, type: :controller do
let(:user) { create(:user) }
let(:link) { create(:link, user: user) }

describe "GET #index" do
it "returns a success response" do
get :index
expect(response).to be_successful
end
end

describe "GET #show" do
it "returns a success response" do
get :show, params: { id: link.to_param }
expect(response).to be_successful
end
end

describe "GET #new" do
it "returns a success response" do
sign_in user
get :new
expect(response).to be_successful
end
end

describe "POST #create" do
context "with valid params" do
it "creates a new Link" do
sign_in user
expect {
post :create, params: { link: attributes_for(:link) }
}.to change(Link, :count).by(1)
end
end
end

describe "PUT #update" do
context "with valid params" do
let(:new_attributes) { { title: "New Title" } }

it "updates the requested link" do
sign_in user
put :update, params: { id: link.to_param, link: new_attributes }
link.reload
expect(link.title).to eq("New Title")
end
end
end

describe "DELETE #destroy" do
it "destroys the requested link" do
sign_in user
link # ensure link is created before the expect block
expect {
delete :destroy, params: { id: link.to_param }
}.to change(Link, :count).by(-1)
end
end

describe "GET #user_links" do
it "returns a success response" do
get :user_links, params: { username: user.username }
expect(response).to be_successful
end

it "assigns the correct instance variables" do
get :user_links, params: { username: user.username }
expect(assigns(:user)).to eq(user)
expect(assigns(:links)).to be_an(ActiveRecord::Relation)
expect(assigns(:pinned_links)).to be_an(ActiveRecord::Relation)
expect(assigns(:achievements)).to be_an(ActiveRecord::Relation)
end
end

describe "GET #track_click" do
it "creates a LinkClick" do
expect {
get :track_click, params: { id: link.to_param }
}.to change(LinkClick, :count).by(1)
end

it "redirects to the link url" do
get :track_click, params: { id: link.to_param }
expect(response).to redirect_to(link.url)
end
end
end
11 changes: 11 additions & 0 deletions spec/controllers/pages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# spec/controllers/pages_controller_spec.rb
require 'rails_helper'

RSpec.describe PagesController, type: :controller do
describe "GET #home" do
it "returns a success response" do
get :home
expect(response).to be_successful
end
end
end
50 changes: 50 additions & 0 deletions spec/controllers/users/registrations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# spec/controllers/users/registrations_controller_spec.rb
require 'rails_helper'

RSpec.describe Users::RegistrationsController, type: :controller do
before do
@request.env["devise.mapping"] = Devise.mappings[:user]
end

describe "POST #create" do
let(:valid_attributes) {
{ email: "[email protected]", password: "password", password_confirmation: "password",
username: "testuser", full_name: "Test User", tags: "tag1,tag2" }
}

it "creates a new User" do
expect {
post :create, params: { user: valid_attributes }
}.to change(User, :count).by(1)
end

it "correctly processes tags" do
post :create, params: { user: valid_attributes }
user = User.last
tags = user.tags.is_a?(String) ? JSON.parse(user.tags) : user.tags
expect(tags).to eq(["tag1", "tag2"])
end
end

describe "PUT #update" do
let(:user) { create(:user, tags: ["old_tag1", "old_tag2"].to_json) }

before do
sign_in user
end

context "with valid params" do
let(:new_attributes) {
{ full_name: "New Name", tags: "new_tag1,new_tag2" }
}

it "updates the requested user" do
put :update, params: { user: new_attributes }
user.reload
expect(user.full_name).to eq("New Name")
tags = user.tags.is_a?(String) ? JSON.parse(user.tags) : user.tags
expect(tags).to eq(["new_tag1", "new_tag2"])
end
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/achievements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
date { Date.today }
description { "This is a test achievement" }
icon { "fa-trophy" }
url { "http://example.com/achievement" }
url { nil }
association :user
end
end
11 changes: 11 additions & 0 deletions spec/factories/daily_metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# spec/factories/daily_metrics.rb
FactoryBot.define do
factory :daily_metric do
user
date { Date.today }
page_views { 10 }
link_clicks { 5 }
achievement_views { 3 }
unique_visitors { 2 }
end
end
26 changes: 25 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require 'rspec/rails'
require 'devise'
require 'factory_bot_rails'
require 'capybara/rspec'
require 'rails-controller-testing'

Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

Expand All @@ -24,6 +26,12 @@
config.include Devise::Test::IntegrationHelpers, type: :request
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view

[:controller, :view, :request].each do |type|
config.include ::Rails::Controller::Testing::TestProcess, :type => type
config.include ::Rails::Controller::Testing::TemplateAssertions, :type => type
config.include ::Rails::Controller::Testing::Integration, :type => type
end

config.include FactoryBot::Syntax::Methods

Expand All @@ -32,11 +40,27 @@
Rails.application.load_tasks
Rake::Task['assets:precompile'].invoke
end

# Clean up uploaded files after each test
config.after(:each) do
FileUtils.rm_rf(Dir["#{Rails.root}/spec/support/uploads"])
end

# Add support for time travel in tests
config.include ActiveSupport::Testing::TimeHelpers
end

Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
end

# Configure Capybara for system tests
Capybara.register_driver :chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new(args: %w[no-sandbox headless disable-gpu])
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.javascript_driver = :chrome_headless

0 comments on commit c375a66

Please sign in to comment.