Testing and Debugging
This section outlines the testing and debugging strategies for GitLab Discussions.
Testing
Unit Tests
Unit tests are used to verify individual components of the application. These tests are located in the spec
directory of each component.
Example:
# spec/models/discussion_spec.rb
require 'rails_helper'
RSpec.describe Discussion, type: :model do
it 'is valid with a title and body' do
discussion = build(:discussion, title: 'My Discussion', body: 'This is my discussion.')
expect(discussion).to be_valid
end
it 'is not valid without a title' do
discussion = build(:discussion, title: nil)
expect(discussion).to be_invalid
end
end
Source: spec/models/discussion_spec.rb
Integration Tests
Integration tests verify the interaction between different components of the application. These tests are located in the spec/integration
directory.
Example:
# spec/integration/discussions_controller_spec.rb
require 'rails_helper'
RSpec.describe DiscussionsController, type: :request do
describe 'GET #index' do
it 'returns a successful response' do
get discussions_path
expect(response).to have_http_status(:success)
end
end
end
Source: spec/integration/discussions_controller_spec.rb
System Tests
System tests are used to verify the application as a whole, including interactions with external services. These tests are located in the spec/system
directory.
Example:
# spec/system/discussions_spec.rb
require 'rails_helper'
RSpec.describe 'Discussions', type: :system do
it 'allows a user to create a new discussion' do
visit discussions_path
click_link 'New Discussion'
fill_in 'Title', with: 'My Discussion'
fill_in 'Body', with: 'This is my discussion.'
click_button 'Create Discussion'
expect(page).to have_content('My Discussion')
end
end
Source: spec/system/discussions_spec.rb
Debugging
Logging
Logging is used to track application events and errors. Logs can be found in the log
directory.
Example:
# app/controllers/discussions_controller.rb
class DiscussionsController < ApplicationController
def create
@discussion = Discussion.new(discussion_params)
if @discussion.save
logger.info "Discussion created: #{@discussion.id}"
redirect_to @discussion
else
logger.error "Discussion creation failed: #{@discussion.errors.full_messages}"
render :new
end
end
end
Source: app/controllers/discussions_controller.rb
Debuggers
Debuggers can be used to step through code execution and inspect variables. The byebug
gem is used for debugging.
Example:
# app/controllers/discussions_controller.rb
class DiscussionsController < ApplicationController
def create
@discussion = Discussion.new(discussion_params)
if @discussion.save
logger.info "Discussion created: #{@discussion.id}"
redirect_to @discussion
else
binding.pry
render :new
end
end
end
Source: app/controllers/discussions_controller.rb
Error Tracking
Error tracking services like Sentry can be used to track and monitor application errors.
Example:
# app/controllers/discussions_controller.rb
class DiscussionsController < ApplicationController
def create
@discussion = Discussion.new(discussion_params)
begin
@discussion.save!
rescue ActiveRecord::RecordInvalid => e
Sentry.capture_exception(e)
render :new
end
end
end