Module: Rspec::ApiHelpers::ExampleGroupMethods

Defined in:
lib/rspec/api_helpers/example_group_methods.rb

Instance Method Summary collapse

Instance Method Details

#it_includes_in_headers(headers = {}) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 68

def it_includes_in_headers(headers = {})
  headers.each do |header, value|
    it "returns headers #{header} wih value: #{value}" do
      expect(last_response.headers[header.to_s]).to eq(eval(value))
    end
  end
end

#it_returns_attribute_values(resource:, model:, only: [], modifier: {}) ⇒ Object Also known as: it_returns_more_attribute_values



10
11
12
13
14
15
16
17
18
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
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 10

def it_returns_attribute_values(resource:, model:, only: [], modifier: {})
  it "expects returned resource (#{resource}) to have model attribute values" do
    api_resource = objectize_resource(last_response.body, root: resource)

    modifier = HashWithIndifferentAccess.new(modifier)

    model = eval(model)
    if model.is_a? Hash
      model = object_hash(model)
    end

    if only
      only.each do |attribute|
        begin
          if modifier.has_key?(attribute)
            modifier[attribute] = [modifier[attribute]].flatten

            expect(api_resource.send(attribute)).to(
              eql(
                modifier[attribute].inject(
                  model.send(attribute), :send
                )
              )
            )
          else
            expect(api_resource.send(attribute)).to(
              eql(model.send(attribute))
            )
          end
        rescue RSpec::Expectations::ExpectationNotMetError => e
          e.message << "failed at model attribute: #{attribute}"
          raise e
        end
      end
    end
  end

end

#it_returns_collection_attributes(resource:, attributes: [], subset: true) ⇒ Object Also known as: it_returns_more_collection_attributes



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 97

def it_returns_collection_attributes(resource:, attributes: [], subset: true)
  it "returns the correct attributes (no value checking) for each resource in the #{resource} collection" do
    resources = objectize_resources(last_response.body, root: resource.pluralize)

    resource_attributes_set = SortedSet.new(
      resources.sample.hash.keys.map(&:to_s)
    )
    checking_attributes_set = SortedSet.new(attributes.map(&:to_s).to_set)

    if subset
      begin
        expect(resource_attributes_set.superset?(checking_attributes_set)).to(
          eq(true)
        )
      rescue RSpec::Expectations::ExpectationNotMetError => _
        raise(
          $!,
          superset_mismatch_error(
            resource_attributes_set, checking_attributes_set
          ),
          $!.backtrace
        )
      end
    else
      expect(resource_attributes_set).to eq(checking_attributes_set)
    end
  end
end

#it_returns_collection_embedded_resource_attributes(resource:, embeds:, attributes: [], subset: true) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 130

def it_returns_collection_embedded_resource_attributes(
  resource:, embeds:, attributes: [], subset: true
)
  it "returns the correct attributes (no value checking) of #{embeds} resource inside #{resource} collection" do
    resource = objectize_resources(last_response.body, root: resource)
    embedded_resource = object_hash(resource.sample.send(embeds.to_sym))

    embedded_resource_attributes_set = SortedSet.new(
      embedded_resource.hash.keys.map(&:to_s)
    )
    checking_attributes_set = SortedSet.new(attributes.map(&:to_s).to_set)

    if subset
      begin
        expect(
          embedded_resource_attributes_set.superset?(
            checking_attributes_set
          )
        ).to(
          eq(true)
        )
      rescue RSpec::Expectations::ExpectationNotMetError => _
        raise(
          $!,
          superset_mismatch_error(
            embedded_resource_attributes_set, checking_attributes_set
          ),
          $!.backtrace
        )
      end
    else
      expect(resource_attributes_set).to eq(checking_attributes_set)
    end
  end

end

#it_returns_collection_embedded_size(resource:, embeds:, size:) ⇒ Object



83
84
85
86
87
88
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 83

def it_returns_collection_embedded_size(resource:, embeds:, size:)
  it "returns the correct number of embedded resource #{embeds} in the #{resource} collection" do
    resources = objectize_resources(last_response.body, root: resource)
    expect(resources.sample[embeds].length).to eql(size)
  end
end

#it_returns_collection_size(resource:, size:) ⇒ Object



76
77
78
79
80
81
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 76

def it_returns_collection_size(resource:, size:)
  it "returns the correct number of resources in the #{resource} collection" do
    resources = objectize_resources(last_response.body, root: resource)
    expect(resources.length).to eql(size)
  end
end

#it_returns_embedded_size(resource:, embeds:, size:) ⇒ Object



90
91
92
93
94
95
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 90

def it_returns_embedded_size(resource:, embeds:, size:)
  it "returns the correct number of embedded resource #{embeds} in the #{resource} resource" do
    resource = objectize_resource(last_response.body, root: resource)
    expect(resource.send(embeds).length).to eql(size)
  end
end

#it_returns_no_attributes(resource:, attributes: []) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 51

def it_returns_no_attributes(resource:, attributes: [])
  it "expects returned resource (#{resource}) to NOT have the following attributes" do
    api_resource = objectize_resource(
      last_response.body, root: resource, existing: false
    )

    attributes.each do |attribute|
      begin
        expect(api_resource.send(attribute)).to eql(:attribute_not_found)
      rescue RSpec::Expectations::ExpectationNotMetError => e
        e.message << "failed at model attribute: #{attribute}"
        raise e
      end
    end
  end
end

#it_returns_no_collection_attributes(resource:, attributes: []) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 167

def it_returns_no_collection_attributes(resource:, attributes: [])
  it "expects returned collection (#{resource}) to NOT have the following attributes" do
    resources = objectize_resources(
      last_response.body, root: resource.pluralize, existing: false
    )

    attributes.each do |attribute|
      begin
        expect(resources.sample.send(attribute)).to eql(:attribute_not_found)
      rescue RSpec::Expectations::ExpectationNotMetError => e
        e.message << "failed at model attribute: #{attribute}"
        raise e
      end
    end
  end
end

#it_returns_no_collection_embedded_resource_attributes(resource:, embeds:, attributes: []) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 184

def it_returns_no_collection_embedded_resource_attributes(
  resource:, embeds:, attributes: []
)
  it "expects the embedded resource #{embeds} inside the returned collection (#{resource}) to NOT have the following attributes" do
    resources = objectize_resources(
      last_response.body, root: resource.pluralize
    )

    embedded_resource = object_hash(
      resources.sample.send(embeds.to_sym), existing: false
    )

    attributes.each do |attribute|
      begin
        expect(embedded_resource.send(attribute)).to eql(:attribute_not_found)
      rescue RSpec::Expectations::ExpectationNotMetError => e
        e.message << "failed at model attribute: #{attribute}"
        raise e
      end
    end
  end
end

#it_returns_status(status) ⇒ Object



4
5
6
7
8
# File 'lib/rspec/api_helpers/example_group_methods.rb', line 4

def it_returns_status(status)
  it "returns the correct HTTP status (status: #{status})" do
    expect(last_response.status).to eql(status)
  end
end