Testing ActiveAgent Applications
Test ActiveAgent applications with credential helpers, VCR for API recording, and proper agent patterns.
Credential Management
Skip tests gracefully when API credentials aren't available:
class MyAgentTest < ActiveSupport::TestCase
test "generates response with OpenAI" do
skip "Requires API credentials" unless has_openai_credentials?
VCR.use_cassette("my_agent_test") do
result = MyAgent.with(input: "test").process.generate_now
assert result.message.content.present?
end
end
endAvailable Helpers
has_openai_credentials? # Rails credentials or ENV vars
has_anthropic_credentials? # Rails credentials or ENV vars
has_openrouter_credentials? # Rails credentials or ENV vars
has_ollama_credentials? # Ollama server configuration
has_provider_credentials?(:openai) # Generic checkCredential Setup
Rails credentials (recommended):
rails credentials:editopenai:
access_token: your-api-key
anthropic:
access_token: your-api-keyEnvironment variables (fallback):
export OPENAI_ACCESS_TOKEN=your-api-key
export ANTHROPIC_ACCESS_TOKEN=your-api-keyTesting Agent Actions
Use the Agent.with(params).action.generate_now pattern:
class MyAgentTest < ActiveSupport::TestCase
test "processes user input" do
skip unless has_openai_credentials?
VCR.use_cassette("my_agent_process") do
result = MyAgent.with(input: "test data").process.generate_now
assert result.message.content.present?
assert_includes result.message.content, "processed"
end
end
endCommon Test Patterns
Basic agent test:
test "support agent responds to help request" do
VCR.use_cassette("support_agent_help") do
response = SupportAgent.with(
user_id: 1,
message: "Need help"
).help.generate_now
assert response.message.content.present?
doc_example_output(response) # Generate docs
end
endTesting with custom configuration:
class ConfigurationTest < ActiveAgentTestCase
test "custom provider settings" do
custom_config = { "openai" => { "model" => "gpt-5" } }
with_active_agent_config(custom_config) do
# Test with custom configuration
end
end
endTesting Concerns
Test that shared functionality is properly included:
test "research agent includes concern tools" do
expected_actions = ["search_papers", "analyze_data"]
agent_actions = ResearchAgent.new.action_methods
expected_actions.each do |action|
assert_includes agent_actions, action
end
endDocumentation Examples
Generate examples from test results:
test "help agent example" do
VCR.use_cassette("help_agent_example") do
result = HelpAgent.with(query: "How do I reset password?").help.generate_now
doc_example_output(result) # Creates example file
assert result.message.content.present?
end
endInclude in documentation:
::: details Response Example
<!-- @include: @/parts/examples/help-agent-test-help-agent-example.md -->
:::Testing Multiple Providers
Test across different providers:
test "works with different providers" do
skip unless has_openai_credentials?
agent_class = Class.new(ApplicationAgent) do
generate_with :openai, model: "gpt-4o"
def analyze; prompt message: "test"; end
end
result = agent_class.with.analyze.generate_now
assert result.message.content.present?
endCommon Issues
Wrong pattern error:
# Wrong - actions don't accept arguments
agent.my_action(param: "value") # Error!
# Right - use with pattern
MyAgent.with(param: "value").my_action.generate_nowMissing credentials:
- Set Rails credentials:
rails credentials:edit - Export environment variables in shell
- Check credential helper method names
VCR cassette issues:
- Delete old cassettes:
rm test/fixtures/vcr_cassettes/test.yml - Re-run tests to record new cassettes
- Verify API credentials are valid
Evals
An eval is a test that calls a model and checks what the agent did — the answer, the tools it called. They are ordinary tests under test/evals; ActiveAgent::Evals::TestCase skips them unless EVALS is on, so rails test never calls a model, and bin/rails test:evals switches them on and runs them, on demand or in a release job:
class SupportAgentEval < ActiveAgent::Evals::TestCase
test "finds an order with the lookup tool" do
replay = run_eval SupportAgent.with(question: "Where is order ABC-123?").help
assert_tool_called replay, "lookup_order", number: "ABC-123"
assert_output_contains replay, "ABC-123"
end
endEvals in Your Test Suite walks through the setup, RSpec, a suite across models, RubyLLM apps, publishing a run to the dashboard and CI. The helper reference is Evaluations → In your test suite.
Test Base Classes
ActiveSupport::TestCase- Standard Rails tests with credential helpersActiveAgentTestCase- For tests needing configuration management