Class: ActionPrompt::Adapters::Test
- Defined in:
- lib/action_prompt/adapters/test.rb
Overview
A test adapter that captures deliveries in memory without making real API calls. Use this in your test suite to assert on prompt content.
Setup (spec/support/action_prompt.rb)
RSpec.configure do |config|
config.before(:each) do
ActionPrompt.reset_configuration!
ActionPrompt::Adapters::Test.clear_deliveries!
end
end
Asserting on deliveries
it "sends the article body in the prompt" do
ArticleSummarizerPrompt.summarize(article).deliver_now
delivery = ActionPrompt::Adapters::Test.deliveries.last
expect(delivery[:prompt]).to include(article.body)
expect(delivery[:options][:model]).to eq("gpt-4o-mini")
end
Class Method Summary collapse
-
.clear_deliveries! ⇒ void
Clears all recorded deliveries.
-
.deliveries ⇒ Array<Hash>
All prompts delivered since the last Test.clear_deliveries! call.
Instance Method Summary collapse
-
#complete(prompt_text, options = {}) ⇒ String
Records the delivery and returns a predictable canned response string.
Class Method Details
.clear_deliveries! ⇒ void
This method returns an undefined value.
Clears all recorded deliveries. Call this in a before(:each) block.
41 42 43 |
# File 'lib/action_prompt/adapters/test.rb', line 41 def clear_deliveries! @deliveries = [] end |
.deliveries ⇒ Array<Hash>
All prompts delivered since the last clear_deliveries! call.
Each entry is a Hash with keys:
- +:prompt+ — the rendered prompt string
- +:options+ — the merged LLM options hash
35 36 37 |
# File 'lib/action_prompt/adapters/test.rb', line 35 def deliveries @deliveries ||= [] end |
Instance Method Details
#complete(prompt_text, options = {}) ⇒ String
Records the delivery and returns a predictable canned response string.
51 52 53 54 55 56 |
# File 'lib/action_prompt/adapters/test.rb', line 51 def complete(prompt_text, = {}) delivery = { prompt: prompt_text, options: } self.class.deliveries << delivery "[ActionPrompt::Test] Response for #{[:model] || "unknown-model"} " \ "(#{prompt_text.length} chars)" end |