Skip to content

Commit 194a0d2

Browse files
committed
test: add RSpec configuration and support files
- Configure RSpec with DatabaseCleaner and FactoryBot - Add SimpleCov for code coverage - Add Shoulda Matchers configuration - Create request spec helper with JWT authentication - Set up test database cleaning strategy
1 parent a31f350 commit 194a0d2

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

spec/rails_helper.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This file is copied to spec/ when you run 'rails generate rspec:install'
2+
require 'spec_helper'
3+
ENV['RAILS_ENV'] ||= 'test'
4+
require_relative '../config/environment'
5+
# Prevent database truncation if the environment is production
6+
abort("The Rails environment is running in production mode!") if Rails.env.production?
7+
require 'rspec/rails'
8+
9+
# Requires supporting ruby files with custom matchers and macros, etc, in
10+
# spec/support/ and its subdirectories.
11+
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
12+
13+
# Checks for pending migrations and applies them before tests are run.
14+
begin
15+
ActiveRecord::Migration.maintain_test_schema!
16+
rescue ActiveRecord::PendingMigrationError => e
17+
abort e.to_s.strip
18+
end
19+
20+
RSpec.configure do |config|
21+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
22+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
23+
24+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
25+
# examples within a transaction, remove the following line or assign false
26+
# instead of true.
27+
config.use_transactional_fixtures = true
28+
29+
# You can uncomment this line to turn off ActiveRecord support entirely.
30+
# config.use_active_record = false
31+
32+
config.infer_spec_type_from_file_location!
33+
34+
# Filter lines from Rails gems in backtraces.
35+
config.filter_rails_from_backtrace!
36+
37+
# Include FactoryBot methods
38+
config.include FactoryBot::Syntax::Methods
39+
40+
# Include request helpers
41+
config.include RequestSpecHelper, type: :request
42+
43+
# Database cleaner
44+
config.before(:suite) do
45+
DatabaseCleaner.clean_with(:truncation)
46+
end
47+
48+
config.before(:each) do
49+
DatabaseCleaner.strategy = :transaction
50+
end
51+
52+
config.before(:each) do
53+
DatabaseCleaner.start
54+
end
55+
56+
config.after(:each) do
57+
DatabaseCleaner.clean
58+
end
59+
end
60+
61+
# Shoulda Matchers configuration
62+
Shoulda::Matchers.configure do |config|
63+
config.integrate do |with|
64+
with.test_framework :rspec
65+
with.library :rails
66+
end
67+
end

spec/spec_helper.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'simplecov'
2+
SimpleCov.start 'rails' do
3+
add_filter '/bin/'
4+
add_filter '/db/'
5+
add_filter '/spec/'
6+
add_filter '/config/'
7+
end
8+
9+
RSpec.configure do |config|
10+
config.expect_with :rspec do |expectations|
11+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
12+
end
13+
14+
config.mock_with :rspec do |mocks|
15+
mocks.verify_partial_doubles = true
16+
end
17+
18+
config.shared_context_metadata_behavior = :apply_to_host_groups
19+
config.filter_run_when_matching :focus
20+
config.example_status_persistence_file_path = "spec/examples.txt"
21+
config.disable_monkey_patching!
22+
23+
if config.files_to_run.one?
24+
config.default_formatter = "doc"
25+
end
26+
27+
config.profile_examples = 10
28+
config.order = :random
29+
Kernel.srand config.seed
30+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module RequestSpecHelper
2+
# Helper method to generate JWT token for testing
3+
def auth_token(user)
4+
Authentication::Services::JwtService.encode(user_id: user.id)
5+
end
6+
7+
# Helper method to set authentication headers
8+
def auth_headers(user)
9+
{
10+
'Authorization' => "Bearer #{auth_token(user)}",
11+
'Content-Type' => 'application/json'
12+
}
13+
end
14+
15+
# Helper to parse JSON response
16+
def json_response
17+
JSON.parse(response.body, symbolize_names: true)
18+
end
19+
end

0 commit comments

Comments
 (0)