Class: SpecForge::Step::Expect

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_forge/step/expect.rb

Overview

Represents an expectation block within a step

Holds the expected status, headers, and body (raw or JSON) that will be validated against the HTTP response. Provides methods to convert expectations into RSpec matchers.

Instance Method Summary collapse

Constructor Details

#initialize(status: nil, headers: nil, raw: nil, json: nil) ⇒ Expect

Returns a new instance of Expect.



13
14
15
16
17
18
19
20
# File 'lib/spec_forge/step/expect.rb', line 13

def initialize(status: nil, headers: nil, raw: nil, json: nil)
  super(
    status: status ? Attribute.from(status) : nil,
    headers: extract_headers(headers),
    raw: raw ? Attribute.from(raw) : nil,
    json: extract_json(json)
  )
end

Instance Method Details

#headers_matcherHash?

Returns headers as a hash of matchers

Returns:

  • (Hash, nil)

    Header matchers keyed by header name



56
57
58
59
60
# File 'lib/spec_forge/step/expect.rb', line 56

def headers_matcher
  return if headers.blank?

  headers.transform_values(&Attribute.resolve_as_matcher_proc)
end

#json_content_matcherRSpec::Matchers::BuiltIn::BaseMatcher?

Returns the JSON content matcher

Returns:

  • (RSpec::Matchers::BuiltIn::BaseMatcher, nil)

    Content matcher or nil



85
86
87
# File 'lib/spec_forge/step/expect.rb', line 85

def json_content_matcher
  json[:content]&.resolve_as_matcher
end

#json_schemaHash?

Returns the JSON schema structure for validation

Returns:

  • (Hash, nil)

    The schema definition or nil



76
77
78
# File 'lib/spec_forge/step/expect.rb', line 76

def json_schema
  json[:schema]
end

#json_size_matcherRSpec::Matchers::BuiltIn::BaseMatcher?

Returns the JSON size matcher

Returns:

  • (RSpec::Matchers::BuiltIn::BaseMatcher, nil)

    Size matcher or nil



67
68
69
# File 'lib/spec_forge/step/expect.rb', line 67

def json_size_matcher
  json[:size]&.resolve_as_matcher
end

#sizeInteger

Returns the total number of assertions in this expectation

Counts all non-blank assertions (status, headers, raw, JSON size/schema/content).

Returns:

  • (Integer)

    Number of assertions



29
30
31
32
33
34
35
36
37
38
# File 'lib/spec_forge/step/expect.rb', line 29

def size
  [
    status,
    headers,
    raw,
    json[:size],
    json[:schema],
    json[:content]
  ].compact_blank.size
end

#status_matcherRSpec::Matchers::BuiltIn::BaseMatcher?

Returns the status code as an RSpec matcher

Returns:

  • (RSpec::Matchers::BuiltIn::BaseMatcher, nil)

    Status matcher or nil



45
46
47
48
49
# File 'lib/spec_forge/step/expect.rb', line 45

def status_matcher
  return if status.blank?

  status.resolve_as_matcher
end