Module: Restspec::RSpec::ApiMacros

Defined in:
lib/restspec/rspec/api_macros.rb

Instance Method Summary collapse

Instance Method Details

#before_test(&block) ⇒ Object



90
91
92
# File 'lib/restspec/rspec/api_macros.rb', line 90

def before_test(&block)
  [:before_callbacks] << block
end

#endpoint(name, options = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/restspec/rspec/api_macros.rb', line 6

def endpoint(name, options = {}, &block)
  endpoint = Restspec::EndpointStore.get(name).dup

  implicit_test = options[:implicit_test]

  self.[:current_endpoint_template] = endpoint
  self.[:resource_endpoint] = Restspec::EndpointStore.get(options[:resource])

  describe "[#{endpoint.method.to_s.upcase} #{endpoint.name}]", options do
    implicit_test ? test(&block) : instance_eval(&block)
  end
end

#ensure!(name) ⇒ Object



113
114
115
116
# File 'lib/restspec/rspec/api_macros.rb', line 113

def ensure!(name)
  requirement = Restspec::Requirements::Requirement.find_by_name(name)
  requirement.assert!
end

#payload(params = {}, &payload_block) ⇒ Object



94
95
96
97
98
99
# File 'lib/restspec/rspec/api_macros.rb', line 94

def payload(params = {}, &payload_block)
  let(:example_payload) do
    payload_params = payload_block.present? ? instance_eval(&payload_block) : {}
    Restspec::Values::SuperHash.new(params.merge(payload_params))
  end
end

#query_params(params = nil, &params_block) ⇒ Object



107
108
109
110
111
# File 'lib/restspec/rspec/api_macros.rb', line 107

def query_params(params = nil, &params_block)
  let(:example_query_params) do
    Restspec::Values::SuperHash.new(params || instance_eval(&params_block))
  end
end

#test(message = 'the happy path', options = {}, &test_body) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/restspec/rspec/api_macros.rb', line 19

def test(message = 'the happy path', options = {}, &test_body)
  endpoint_context = self
  endpoint_context.[:before_callbacks] = []

  context(message, options) do
    test_context = self

    let(:endpoint) do
      endpoint_block_endpoint = endpoint_context.[:current_endpoint_template]
      test_context.[:current_endpoint] ||= endpoint_block_endpoint.clone
    end

    before { response }

    let(:response) do
      @response = execute_endpoint_lambda.call
    end

    let(:request) do
      endpoint.last_request
    end

    let(:resource_endpoint) do
      test_context.[:resource_endpoint]
    end

    let(:initial_resource) do
      test_context.[:initial_resource] ||= begin
        resource_endpoint.try(:execute).try(:body)
      end
    end

    let(:final_resource) { body }

    let(:execute_endpoint_lambda) do
      resource_params = resource_endpoint.try(:url_params) || {}
      before_callbacks = test_context.[:before_callbacks]

      -> do
        endpoint.execute_once(
          body: payload,
          url_params: url_params.merge(resource_params).merge(@url_params || {}),
          query_params: query_params.merge(@query_params || {}),
          before: ->do
            before_callbacks.each { |callback| instance_eval(&callback) }
            initial_resource
          end
        )
      end
    end

    let(:body) { response.body }

    subject { response }

    let(:payload) do
      defined?(example_payload) ? example_payload : nil
    end

    let(:url_params) do
      defined?(example_url_params) ? example_url_params : {}
    end

    let(:query_params) do
      defined?(example_query_params) ? example_query_params : {}
    end

    instance_eval(&test_body)
  end
end

#url_params(params = nil, &params_block) ⇒ Object



101
102
103
104
105
# File 'lib/restspec/rspec/api_macros.rb', line 101

def url_params(params = nil, &params_block)
  let(:example_url_params) do
    Restspec::Values::SuperHash.new(params || instance_eval(&params_block))
  end
end

#within_response(&block) ⇒ Object



118
119
120
121
122
123
# File 'lib/restspec/rspec/api_macros.rb', line 118

def within_response(&block)
  context 'within response' do
    subject { response.body }
    instance_eval(&block)
  end
end