# Testing Patterns

*Note: This is a simplified example from a working project, adapted for demonstration purposes.*

## Core Principles

- Minitest with spec syntax
- Fixtures, not factories
- TDD is mandatory - tests before implementation
- Test behavior, not Rails

## Test Structure

```ruby
# ALWAYS use class-based structure
class UserTest < ActiveSupport::TestCase
  describe User do
    subject { users(:example) }  # Use fixtures

    describe 'validations' do
      it 'requires email' do
        subject.email = nil
        assert_not subject.valid?
        assert_includes subject.errors[:email], "can't be blank"
      end
    end
  end
end
```

## Anti-Patterns (NEVER DO)

### Testing Rails Framework
```ruby
# BAD - Testing Rails
it 'has an email attribute' do
  assert_respond_to user, :email
end

# GOOD - Test business logic
it 'requires valid email format' do
  user.email = 'invalid'
  assert_not user.valid?
end
```

### Tests That Can't Fail
```ruby
# BAD - Always passes
it 'creates a user' do
  user = User.new(name: 'Test')
  assert user  # This just tests Ruby truthiness!
end

# GOOD - Actually tests something
it 'creates a valid user' do
  user = User.create!(valid_attributes)
  assert user.persisted?
  assert_equal 'Test', user.name
end
```

## Quick Checklist

Before writing any test:
1. Am I testing Rails itself? (STOP)
2. Would this test fail if I changed MY code? (If no, STOP)
3. Does this test provide value? (If no, STOP)

## Running Tests

```bash
bin/rails test                           # All tests
bin/rails test test/models/user_test.rb  # Specific file
bin/rails test test/models/user_test.rb -n /email/  # Pattern match
```