TNG - AI-Powered Test Generation for Rails

TNG is an AI-powered test generation tool that automatically creates comprehensive tests for your Rails applications. It analyzes your code structure, authentication patterns, and configuration to generate high-quality, contextual tests that follow your project's conventions.

Owner: Binary Dreams LLC

Installation

Standard Installation

Add this line to your application's Gemfile:

gem 'tng'

And then execute:

bundle install

Configuration

TNG requires proper configuration to generate high-quality tests. Generate the configuration file by running:

rails generate tng:install

This creates config/initializers/tng.rb with a configuration template that you'll need to customize for your application. The generator will attempt to detect your existing setup and pre-configure common settings.

Configuration Options Explained

API Configuration

  • api_key: Your TNG API key from https://app.tng.sh/
  • base_url: TNG service endpoint (automatically configured, do not modify)

Code Reading

  • read_source_code: Analyzes the specific code file for which tests are being generated
  • read_test_code: Reads existing tests for the specific file to match your testing patterns

Testing Framework Settings

  • testing_framework: Automatically detected. Possible values: minitest or rspec

Note: The following settings depend on your testing framework.

For Minitest:

  • test_style: Test organization style. Possible options:
    • test_block - test "does something" do ... end
    • spec - it "does something" do ... end
    • unit - def test_does_something ... end
  • assertion_style: Assertion syntax preference. Possible values:
    • assert/refute - Uses syntax like assert, assert_equal, assert_nil, refute, refute_nil
    • assert/assert_not - Uses syntax like assert, assert_equal, assert_nil, assert_not, assert_not_equl, assert_not_nil
    • must/wont - value.must_equal, value.must_be_instance_of, value.must_raise, value.wont_equal, value.wont_be_instance_of, value.wont_raise

For RSpec:

  • describe_style: Include describe blocks. Possible values: true, false
  • context_style: Context block style. Possible values: context, describe
  • it_style: Example block style. Possible values: it, specify
  • before_style: Setup block style. Possible values: before, setup
  • after_style: Teardown block style. Possible values: after, teardown
  • let_style: Use let helpers. Possible values: true, false
  • subject_style: Use subject helpers. Possible values: true, false

Authentication Methods

Supported authentication types:

  • session: Session-based authentication
  • devise: Devise gem integration
  • jwt: JSON Web Token authentication
  • token_auth: Token-based authentication
  • basic_auth: HTTP Basic Authentication
  • oauth: OAuth authentication
  • headers: Custom header authentication
  • custom: Custom authentication logic

Mock and Factory Libraries

  • mock_library: Mock library preference
    • For Minitest: minitest/mock, mocha, nil
    • For RSpec: rspec-mocks, mocha, flexmock, nil
  • http_mock_library: HTTP mocking library. Possible values: webmock, vcr, httparty, nil
  • factory_library: Factory library preference. Possible values: factory_bot, factory_girl, fabrication, fabricator, fixtures, active_record

Configuration Examples

Minitest with FactoryBot Setup

# config/initializers/tng.rb
Tng.configure do |config|
  config.api_key = ENV["TNG_API_KEY"]
  config.base_url = "https://app.tng.sh/"

  config.read_source_code = true
  config.read_test_code = true

  config.testing_framework = "minitest"
  config.test_style = "test_block"
  config.assertion_style = "assert/refute"
  config.setup_style = true
  config.teardown_style = false

  config.mock_library = "minitest/mock"
  config.http_mock_library = "webmock"
  config.factory_library = "factory_bot"

  config.authentication_enabled = true
  config.authentication_methods = [
    {
      method: "authenticate_user!",
      file_location: "app/controllers/application_controller.rb",
      auth_type: "session"
    }
  ]
end

RSpec with FactoryBot Setup

# config/initializers/tng.rb
Tng.configure do |config|
  config.api_key = ENV["TNG_API_KEY"]
  config.base_url = "https://app.tng.sh/"

  config.testing_framework = "rspec"
  config.assertion_style = "expect"
  config.describe_style = true
  config.context_style = "context"
  config.it_style = "it"
  config.before_style = "before"
  config.after_style = "after"
  config.let_style = true
  config.subject_style = true

  config.mock_library = "rspec-mocks"
  config.http_mock_library = "webmock"
  config.factory_library = "factory_bot"

  config.authentication_enabled = true
  config.authentication_methods = [
    {
      method: "authenticate_user!",
      file_location: "app/controllers/application_controller.rb",
      auth_type: "devise"
    }
  ]
end

API-Only Application with Token Authentication

# config/initializers/tng.rb
Tng.configure do |config|
  config.api_key = ENV["TNG_API_KEY"]
  config.base_url = "https://app.tng.sh/"

  config.read_source_code = true
  config.read_test_code = true

  config.testing_framework = "minitest"
  config.test_style = "test_block"
  config.assertion_style = "assert/refute"

  config.mock_library = "minitest/mock"
  config.http_mock_library = "webmock"
  config.factory_library = "fixtures"

  config.authentication_enabled = true
  config.authentication_methods = [
    {
      method: "authenticate_via_token!",
      file_location: "app/controllers/application_controller.rb",
      auth_type: "headers"
    }
  ]
end

Multiple Authentication Methods

# config/initializers/tng.rb
Tng.configure do |config|
  config.api_key = ENV["TNG_API_KEY"]
  config.base_url = "https://app.tng.sh/"
  config.testing_framework = "rspec"
  config.assertion_style = "expect"
  config.let_style = true

  config.mock_library = "rspec-mocks"
  config.http_mock_library = "webmock"
  config.factory_library = "factory_bot"

  config.authentication_enabled = true
  config.authentication_methods = [
    {
      method: "authenticate_user!",
      file_location: "app/controllers/application_controller.rb",
      auth_type: "session"
    },
    {
      method: "authenticate_api_user!",
      file_location: "app/controllers/api/base_controller.rb",
      auth_type: "token_auth"
    }
  ]

  config.authorization_library = "pundit"
end

Advanced Configuration

Custom Authentication Setup

For applications with custom authentication logic that doesn't fit standard patterns:

config.authentication_methods = [
  {
    method: "verify_custom_auth!",
    file_location: "app/controllers/concerns/custom_auth.rb",
    auth_type: "custom"
  }
]

Rails API Application

config.authentication_enabled = true
config.authentication_methods = [
  {
    method: "authenticate_with_token",
    file_location: "app/controllers/application_controller.rb",
    auth_type: "jwt"
  }
]
config.factory_library = "fixtures"  # Often simpler for API apps

Multi-Tenant Application

config.authentication_methods = [
  {
    method: "authenticate_tenant!",
    file_location: "app/controllers/application_controller.rb",
    auth_type: "custom"
  },
  {
    method: "authenticate_user!",
    file_location: "app/controllers/application_controller.rb",
    auth_type: "devise"
  }
]

Legacy Application with Mixed Authentication

config.authentication_methods = [
  {
    method: "authenticate_admin!",
    file_location: "app/controllers/admin/base_controller.rb",
    auth_type: "basic_auth"
  },
  {
    method: "authenticate_user!",
    file_location: "app/controllers/application_controller.rb",
    auth_type: "session"
  },
  {
    method: "authenticate_api_key!",
    file_location: "app/controllers/api/v1/base_controller.rb",
    auth_type: "headers"
  }
]

Usage Patterns

Interactive Mode

The recommended way to use TNG for beginners and exploratory testing:

bundle exec tng

This launches an interactive session where you can:

  • Browse your application structure
  • Select files to generate tests for
  • Preview generated tests before saving
  • Adjust configuration on the fly

Method-Specific Testing

TNG focuses on precise, method-level test generation:

  • Select specific methods from 20+ file types including controllers, models, services, jobs, helpers, lib, policies, presenters, mailers, GraphQL components, and more
  • Interactive browsing with search and filter capabilities
  • Focused test generation for individual methods only
  • No bulk generation - intentionally designed for precision

Use bundle exec tng --help for more options.

Direct Mode (for Developers)

For experienced developers who want quick, command-line test generation:

# Generate test for specific method using file path
bundle exec tng app/controllers/users_controller.rb index

# Short form using shortcuts
bundle exec tng f=users_controller.rb m=show

# Full parameter names
bundle exec tng --file=app/models/user.rb --method=validate

# Works from subdirectories (auto-detection)
bundle exec tng users_controller.rb index

# Mixed parameter order
bundle exec tng m=index f=app/controllers/users_controller.rb

Direct Mode Features:

  • Automatic file type detection - No need to specify -t controller/model/service
  • Flexible file path resolution - Works with full paths or just filenames
  • Multiple syntax formats - Use f= shortcuts or --file= full parameters
  • Smart path finding - Automatically searches Rails directories

Debug Mode

For troubleshooting configuration or generation issues:

# Interactive mode with debug on
DEBUG=1 bundle exec tng

# Direct mode with debug
DEBUG=1 bundle exec tng app/controllers/users_controller.rb index

Help and Options

bundle exec tng --help              # Show help message
bundle exec tng -h                  # Short help

Troubleshooting

Configuration Issues

API Key Problems

Error: "Invalid API key"

  • Verify your API key from https://app.tng.sh/
  • Check environment variable: echo $TNG_API_KEY
  • Ensure no extra spaces in .env file: TNG_API_KEY=your_key_here

Error: "API key not found"

  • Set environment variable: export TNG_API_KEY=your_key
  • Or add to .env file: TNG_API_KEY=your_key
  • Restart Rails server after changing environment variables

Configuration Validation

Error: "Invalid configuration"

  • Run configuration check: rails console then Tng.configuration
  • Verify all required settings are present
  • Check for typos in configuration file

Poor Test Quality

  • Verify authentication methods are correctly configured
  • Check factory library setting matches your setup
  • Ensure read_source_code and read_test_code are enabled
  • Review testing framework settings match your project

Authentication Configuration Issues

Tests don't include authentication

  • Verify authentication_enabled = true
  • Check authentication methods are correctly specified
  • Ensure file paths exist: ls app/controllers/application_controller.rb
  • Verify method names exist in specified files

Authentication tests fail

  • Check auth method names match your application
  • Verify file locations are correct
  • Ensure auth_type matches your authentication pattern

Generation Issues

No Tests Generated

Check these common issues:

  1. File path is correct: ls app/controllers/users_controller.rb
  2. File contains valid Ruby code
  3. Configuration is valid
  4. API key is working
  5. Internet connection is available

Generated Tests Are Generic

Common causes:

  • Authentication not properly configured
  • Factory library setting doesn't match your setup
  • read_source_code or read_test_code disabled
  • File analysis failed (check DEBUG mode)

Debug Mode

Enable debug mode to get detailed information:

DEBUG=1 bundle exec tng app/controllers/your_controller.rb method_name

Debug output includes:

  • API request/response information

Getting Help

If you're still experiencing issues: Contact the gem maintainers or open a GitHub issue for support

License

Copyright (c) 2025 Binary Dreams LLC. All rights reserved.

See LICENSE.md for details.