Module: RubyLLM::Test
- Defined in:
- lib/ruby_llm/test.rb,
lib/ruby_llm/test/harness.rb,
lib/ruby_llm/test/version.rb,
lib/ruby_llm/test/test_provider.rb,
lib/ruby_llm/test/complete_parameters.rb,
lib/ruby_llm/test/resolve_with_test_provider.rb,
lib/ruby_llm/test/errors/no_response_provided_error.rb
Overview
The Test module provides a simple way to stub responses from an LLM for testing purposes. You can use it to set up predetermined responses that your tests can rely on, allowing you to test your code’s behavior without making actual calls to an LLM.
Defined Under Namespace
Modules: Errors, ResolveWithTestProvider Classes: CompleteParameters, Harness, TestProvider
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
-
.clear_requests ⇒ Object
Clear all recorded requests.
-
.last_request ⇒ Object
Return the most recent request.
-
.requests ⇒ Object
Return all recorded requests.
-
.reset ⇒ Object
Reset the test harness state.
-
.stub_response(body) ⇒ Object
Queue a single stubbed response.
-
.stub_responses(*bodies) ⇒ Object
Queue multiple stubbed responses.
-
.with_responses(*bodies) ⇒ Object
Run a block with a temporary set of stubbed responses.
Class Method Details
.clear_requests ⇒ Object
Clear all recorded requests.
This leaves queued responses unchanged.
111 112 113 |
# File 'lib/ruby_llm/test.rb', line 111 def clear_requests harness.clear_requests end |
.last_request ⇒ Object
Return the most recent request.
Use this when you only care about the latest request made during a test.
104 105 106 |
# File 'lib/ruby_llm/test.rb', line 104 def last_request harness.last_request end |
.requests ⇒ Object
Return all recorded requests.
This is useful for assertions about prompts, parameters, or other request details captured by the harness.
97 98 99 |
# File 'lib/ruby_llm/test.rb', line 97 def requests harness.requests end |
.reset ⇒ Object
Reset the test harness state.
This clears all queued stubbed responses and all recorded requests.
Use this at the start of a test, or whenever you want to ensure no state carries over from a previous example.
21 22 23 |
# File 'lib/ruby_llm/test.rb', line 21 def reset harness.reset end |
.stub_response(body) ⇒ Object
Queue a single stubbed response.
Parameters:
-
‘body`: The response to queue. Pass a `RubyLLM::Message` to control the message directly. Strings and hashes are wrapped in a message automatically.
This is useful when your test only needs one response from the model.
Example:
RubyLLM::Test.stub_response("Hello from the test harness!")
chat = RubyLLM.chat
response = chat.ask("Say hello")
response.content
# => "Hello from the test harness!"
43 44 45 |
# File 'lib/ruby_llm/test.rb', line 43 def stub_response(body) harness.stub_response(body) end |
.stub_responses(*bodies) ⇒ Object
Queue multiple stubbed responses.
Parameters:
-
‘*bodies`: One or more responses to queue.
Responses are returned in the same order they are provided, making this useful for tests that perform multiple LLM calls in sequence.
Example:
RubyLLM::Test.stub_responses("First reply", "Second reply")
chat = RubyLLM.chat
first_response = chat.ask("First question")
second_response = chat.ask("Second question")
first_response.content
# => "First reply"
second_response.content
# => "Second reply"
68 69 70 |
# File 'lib/ruby_llm/test.rb', line 68 def stub_responses(*bodies) harness.stub_responses(*bodies) end |
.with_responses(*bodies) ⇒ Object
Run a block with a temporary set of stubbed responses.
Parameters:
-
‘*bodies`: The responses to make available inside the block.
-
‘&block`: The code to run while those responses are active.
The provided responses are available only for the duration of the block. This is useful when you want to scope stubbed responses to a single part of a test without affecting later assertions.
Example:
RubyLLM::Test.with_responses("Scoped reply") do
chat = RubyLLM.chat
chat.ask("Question")
end
89 90 91 |
# File 'lib/ruby_llm/test.rb', line 89 def with_responses(*bodies, &) harness.with_responses(*bodies, &) end |