-
Notifications
You must be signed in to change notification settings - Fork 0
Testing Guide
This page explains how to run, write, and maintain tests for the StateForce project using Minitest, the default testing framework in Ruby on Rails.
- Confidence: Ensure that new changes don't break existing functionality.
- Documentation: Tests serve as living documentation for how components are expected to behave.
- Refactoring: Enable safe refactoring of code with a safety net.
- Design: Writing tests first (TDD) can help drive better software design.
StateForce uses the following test types, located under the test directory:
-
Scope: Models, services, and validators (
test/models/,test/services/,test/validators/). - Purpose: Verify that individual components work correctly in isolation.
- Speed: Fastest to run.
-
Scope: Controllers, mailers, jobs, and routes (
test/controllers/,test/jobs/,test/routes/). - Purpose: Ensure multiple parts of the app integrate and communicate as expected.
- Speed: Slower than unit tests, faster than system tests.
-
Scope: End-to-end user flows, simulated via a browser (
test/system/). - Purpose: Verify complete user interactions with the UI.
- Speed: Slowest to run due to browser involvement.
rails testrails test:system # System tests
rails test:models # Model tests
rails test:controllers # Controller testsrails test test/models/user_test.rbrails test test/models/user_test.rb:15rails test test/models/user_test.rb -n /test_should_be_valid/rails db:test:preparerails db:create RAILS_ENV=testrails db:migrate RAILS_ENV=testrails db:setup RAILS_ENV=test- Arrange, Act, Assert (AAA): Structure tests into setup, execution, and verification phases.
-
Descriptive Test Names: Use
test "description of behavior" do ... end. - Independence: Tests should not rely on each other or the order they are run.
- Test Behavior, Not Implementation: Focus on what the code does, not how it does it.
Model Test (Unit):
require "test_helper"
class UserTest < ActiveSupport::TestCase
setup do
@valid_attributes = { name: "Martin", email: "hey@martinmendoza.dev", password: "password123" }
end
test "should be valid with valid attributes" do
user = User.new(@valid_attributes)
assert user.valid?, "User should be valid with all required attributes."
end
endController Test (Integration):
require "test_helper"
class PostsControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:martin)
sign_in @user
end
test "should get index" do
get posts_url
assert_response :success
end
endSystem Test:
require "application_system_test_case"
class UserLoginTest < ApplicationSystemTestCase
test "user logs in successfully" do
visit login_path
fill_in "Email", with: "test@example.com"
fill_in "Password", with: "password"
click_on "Log in"
assert_text "Welcome back!"
end
end- Located in
test/fixtures/*.yml. - Access via
users(:martin)orposts(:one).
- If adopted, factories provide greater flexibility for creating test objects.
- Use SimpleCov to measure code coverage.
rails test
open coverage/index.html- CI runs the full test suite for all pull requests using GitHub Actions.
- All tests must pass before merging.
- Use
putsorRails.logger.debugfor debugging. - System Test Screenshots: Configure Capybara to save screenshots on failure.
-
Pending Migrations: Run
rails db:migrate RAILS_ENV=test. -
Fixture Errors: Ensure associations and validations are correct in
.ymlfiles. - System Tests Flakiness: Update browser drivers and increase Capybara's wait time.
This guide provides all the tools necessary for writing reliable and scalable tests within the StateForce project. For suggestions or improvements, feel free to open a GitHub issue.
We are always looking for feedback and contributions to improve StateForce. Feel free to open issues, suggest edits, or submit pull requests to the repository.
- Repo: StateForce GitHub
- Contact: martinmendozadev@gmail.com
If you encounter issues or need assistance, please reach out via:
- Email: martinmendozadev@gmail.com
- GitHub Issues: Submit an Issue
This documentation and the StateForce platform are licensed under the Apache License 2.0.
Thank you for using StateForce! π¨
Together, we are improving emergency response operations in real-time.
Welcome to the StateForce Wiki! Use this sidebar to navigate through the documentation.
This sidebar provides quick access to all the documentation pages. For detailed information, click on the desired section.