module RubyLLM::TestHelper

Lets a test script what the model replies, with no provider and no API key. Replies go through the real conversation loop, so streaming, tools, persistence, cost, and #cancel all behave as they do in production. Require it in your test helper, and include it like ActiveJob::TestHelper:

require "ruby_llm/test_helper"

class SupportAgentTest < ActiveSupport::TestCase
  include RubyLLM::TestHelper

  test "answers" do
    stub_replies "Hello there", cost: 0.01

    reply = SupportAgent.new.ask("Hi")

    assert_equal "Hello there", reply.content
    assert_equal 0.01, reply.cost.total
  end
end

It also works in RSpec: config.include RubyLLM::TestHelper. A chat’s request reaches the stub. Other operations, such as RubyLLM.embed, still call their provider.