Class: ResourceSet::Testing::ActionHandlerMatchers

Inherits:
Object
  • Object
show all
Defined in:
lib/resource_set/testing/action_handler_matchers.rb

Constant Summary collapse

ResponseStub =
Class.new(OpenStruct)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ ActionHandlerMatchers

Returns a new instance of ActionHandlerMatchers.



10
11
12
# File 'lib/resource_set/testing/action_handler_matchers.rb', line 10

def initialize(action)
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



8
9
10
# File 'lib/resource_set/testing/action_handler_matchers.rb', line 8

def action
  @action
end

#failure_messageObject (readonly)

Returns the value of attribute failure_message.



8
9
10
# File 'lib/resource_set/testing/action_handler_matchers.rb', line 8

def failure_message
  @failure_message
end

#response_stubObject (readonly)

Returns the value of attribute response_stub.



8
9
10
# File 'lib/resource_set/testing/action_handler_matchers.rb', line 8

def response_stub
  @response_stub
end

Instance Method Details

#matches?(subject, &block) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/resource_set/testing/action_handler_matchers.rb', line 21

def matches?(subject, &block)
  @handled_block ||= block
  action = subject.resources.find_action(self.action)
  unless action
    @failure_message = "expected :#{self.action} to be handled by the class."
    return false
  end

  status_code = response_stub.status || 200
  unless action.handlers[status_code]
    @failure_message = "expected the #{status_code} status code to be handled by the class."
    return false
  end

  handled_response = action.handlers[status_code].call(response_stub)
  @handled_block.call(handled_response)

  true
end

#with(options, &block) ⇒ Object



14
15
16
17
18
19
# File 'lib/resource_set/testing/action_handler_matchers.rb', line 14

def with(options, &block)
  @response_stub = ResponseStub.new(options)
  @handled_block = block

  self
end