Skip to content

feat(testing): add centralized test helper modules (#6)#7

Merged
rakshans1 merged 1 commit intomainfrom
test-helpers
Dec 25, 2025
Merged

feat(testing): add centralized test helper modules (#6)#7
rakshans1 merged 1 commit intomainfrom
test-helpers

Conversation

@rakshans1
Copy link
Owner

PR-06: Centralized Test Helpers

Overview

Creates centralized helper modules for cleaner, more maintainable tests.

Patterns Demonstrated

  • StubHelper for centralized mock configuration
  • AssertHelper for custom domain assertions
  • SetupHelper for reusable setup functions
  • Composable setup with named functions

Files Added

  • test/support/stub_helper.ex - Stub configuration helpers
  • test/support/assert_helper.ex - Custom assertions
  • test/support/setup_helper.ex - Reusable setup functions
  • test/ex_test/helpers_demo_test.exs - Demo test (22 tests)

Files Modified

  • test/support/data_case.ex - Import all helper modules

Usage Example

# Composable setup
setup [:with_email_success, :with_todos, :with_completed_todo]

test "example", %{todos: todos, completed_todo: completed} do
  # Use AssertHelper for validations
  assert_todo_valid(completed)
  assert_count(todos, 3)
  
  # Chain assertions
  completed
  |> assert_todo_valid()
  |> assert_todo_completed()
end

How to Run

mix test test/ex_test/helpers_demo_test.exs

Blog Post

This PR corresponds to Part 6: Centralized Test Helpers.

Next Steps

PR-07 will introduce Phoenix controller testing.

Introduces centralized helper modules for cleaner, more maintainable tests:

- StubHelper: Centralized stub configuration (with_email_success, with_storage_failure, etc.)
- AssertHelper: Domain-specific assertions (assert_todo_valid, assert_changeset_error, etc.)
- SetupHelper: Reusable setup functions (with_todo, with_mixed_todos, etc.)

All helpers are auto-imported via DataCase for seamless usage.

Patterns Demonstrated:
- Composable setup with named functions: setup [:with_email_success, :with_todos]
- Assertion chaining: todo |> assert_todo_valid() |> assert_todo_incomplete()
- Context-aware setup functions for ExUnit's setup macro

Blog Post: Part 6 - Centralized Test Helpers
@rakshans1 rakshans1 merged commit a2592be into main Dec 25, 2025
1 check passed
rakshans1 added a commit that referenced this pull request Dec 25, 2025
Introduces Phoenix controller testing with ConnCase:

- User schema and context for auth simulation
- TodoController with full CRUD operations
- TodoJSON for JSON rendering
- FallbackController for error handling
- ConnSimulator for authentication helpers
- Comprehensive controller tests

Patterns Demonstrated:
- ConnCase template usage
- JSON response assertions
- Testing CRUD operations
- Error case testing (404, 422)
- Authentication simulation

Blog Post: Part 7 - Testing Phoenix Controllers
rakshans1 added a commit that referenced this pull request Dec 25, 2025
Introduces Phoenix controller testing with ConnCase:

- User schema and context for auth simulation
- TodoController with full CRUD operations
- TodoJSON for JSON rendering
- FallbackController for error handling
- ConnSimulator for authentication helpers
- Comprehensive controller tests

Patterns Demonstrated:
- ConnCase template usage
- JSON response assertions
- Testing CRUD operations
- Error case testing (404, 422)
- Authentication simulation

Blog Post: Part 7 - Testing Phoenix Controllers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments