Class: RubyLLM::Test::Harness

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/test/harness.rb

Overview

This class serves as a test harness for stubbing responses and recording requests sent to the provider. It allows tests to set up expected responses and then verify that the provider received the correct parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHarness

Returns a new instance of Harness.



11
12
13
# File 'lib/ruby_llm/test/harness.rb', line 11

def initialize
  reset
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



9
10
11
# File 'lib/ruby_llm/test/harness.rb', line 9

def requests
  @requests
end

Instance Method Details

#clear_requestsObject



49
50
51
# File 'lib/ruby_llm/test/harness.rb', line 49

def clear_requests
  requests.clear
end

#last_requestObject



45
46
47
# File 'lib/ruby_llm/test/harness.rb', line 45

def last_request
  requests.last
end

#next_responseObject



33
34
35
# File 'lib/ruby_llm/test/harness.rb', line 33

def next_response
  responses.shift
end

#record_request(request) ⇒ Object



41
42
43
# File 'lib/ruby_llm/test/harness.rb', line 41

def record_request(request)
  requests << request
end

#resetObject



53
54
55
56
# File 'lib/ruby_llm/test/harness.rb', line 53

def reset
  @responses = []
  @requests = []
end

#responses_empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ruby_llm/test/harness.rb', line 37

def responses_empty?
  responses.empty?
end

#stub_response(body) ⇒ Object

Pass in a RubyLLM::Message to have full control; strings and hashes will be wrapped in a message



16
17
18
# File 'lib/ruby_llm/test/harness.rb', line 16

def stub_response(body)
  responses << body
end

#stub_responses(*bodies) ⇒ Object



20
21
22
# File 'lib/ruby_llm/test/harness.rb', line 20

def stub_responses(*bodies)
  responses.concat(bodies)
end

#with_responses(*bodies) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/ruby_llm/test/harness.rb', line 24

def with_responses(*bodies)
  previous_responses = responses.dup
  @responses = []
  stub_responses(*bodies)
  yield
ensure
  @responses = previous_responses
end