Module: Intermodal::RSpec::Resources::ClassMethods
- Defined in:
- lib/intermodal/rspec/requests/resources.rb
Constant Summary collapse
- STANDARD_SUCCESSFUL_STATUS_FOR =
{ :index => 200, :show => 200, :create => 201, :update => 200, :destroy => 204 }
- STANDARD_REQUEST_FOR =
{ :index => { :method => :get, :end_point => :collection_url }, :show => { :method => :get, :end_point => :resource_url }, :create => { :method => :post, :end_point => :collection_url, :payload => proc do valid_create_attributes end }, :update => { :method => :put, :end_point => :resource_url, :payload => proc do valid_update_attributes end }, :destroy => { :method => :delete, :end_point => :resource_url } }
Instance Method Summary collapse
- #collection_url_for_resource(namespace, parents, resource_name, format) ⇒ Object
- #expects_create(options = {}, &additional_examples) ⇒ Object
- #expects_destroy(options = {}, &additional_examples) ⇒ Object
- #expects_index(options = {}, &additional_examples) ⇒ Object
- #expects_json_presentation(_presenter_scope = nil) ⇒ Object
- #expects_resource_crud(options = {}, &additional_examples) ⇒ Object
- #expects_show(options = {}, &additional_examples) ⇒ Object
- #expects_update(options = {}, &additional_examples) ⇒ Object
- #given_create_attributes(values = {}) ⇒ Object
- #given_update_attributes(values = {}) ⇒ Object
- #metadata_for_formatted_resource(format, options = {}) ⇒ Object
- #metadata_for_resources(resource_name, options = {}) ⇒ Object
- #parent_path_for_resource(parents) ⇒ Object
- #request_resource_action(action, options = {}, &blk) ⇒ Object
- #resource_url_for_resource(namespace, parents, resource_name, format) ⇒ Object
- #resources(resource_name, options = {}, &blk) ⇒ Object
- #with_malformed_data_should_respond_with_400 ⇒ Object
- #with_non_existent_resource_should_respond_with_404 ⇒ Object
Instance Method Details
#collection_url_for_resource(namespace, parents, resource_name, format) ⇒ Object
107 108 109 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 107 def collection_url_for_resource(namespace, parents, resource_name, format) [ namespace, parent_path_for_resource(parents), "/#{resource_name}.#{format}" ].join end |
#expects_create(options = {}, &additional_examples) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 208 def expects_create( = {}, &additional_examples) request_resource_action(:create, ) do it "should return the newly created #{[:resource_name]}" do body.should eql(parser.decode(resource_after_create.send("to_#{format}", { :presenter => presenter, :scope => presenter_scope}))) end with_malformed_data_should_respond_with_400 instance_eval(&additional_examples) if additional_examples end end |
#expects_destroy(options = {}, &additional_examples) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 236 def expects_destroy( = {}, &additional_examples) request_resource_action(:destroy, {mime_type: nil, encoding: nil}.merge()) do it "should delete #{[:resource_name]}" do response.should_not be(nil) lambda { resource_after_destroy }.should raise_error(record_not_found_error) end with_non_existent_resource_should_respond_with_404 instance_eval(&additional_examples) if additional_examples end end |
#expects_index(options = {}, &additional_examples) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 175 def expects_index( = {}, &additional_examples) request_resource_action(:index, ) do unless [:skip_presentation_test] it "should return a list of all #{[:resource_name]}" do reset_datastore! collection.should_not be_empty body.should eql(presented_collection) end end unless [:skip_pagination_examples] expects_paginated_resource do before(:each) { reset_datastore! } end end instance_eval(&additional_examples) if additional_examples end end |
#expects_json_presentation(_presenter_scope = nil) ⇒ Object
265 266 267 268 269 270 271 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 265 def expects_json_presentation(_presenter_scope = nil) let(:presenter_scope) { _presenter_scope } if _presenter_scope it 'should present a JSON object' do body.should eql(presented_resource) end end |
#expects_resource_crud(options = {}, &additional_examples) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 133 def expects_resource_crud( = {}, &additional_examples) expected_actions = [:only] || [ :index, :show, :create, :update, :destroy ] expected_actions -= [:except] if [:except] expected_actions.each do |action| send("expects_#{action}") end instance_eval(&additional_examples) if additional_examples end |
#expects_show(options = {}, &additional_examples) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 195 def expects_show( = {}, &additional_examples) request_resource_action(:show, ) do it "should return a #{[:resource_name]} of id 1" do resource.should_not be_nil body.should eql(resource) end with_non_existent_resource_should_respond_with_404 instance_eval(&additional_examples) if additional_examples end end |
#expects_update(options = {}, &additional_examples) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 220 def expects_update( = {}, &additional_examples) request_resource_action(:update, ) do it "should update #{[:resource_name]}" do response.should_not be(nil) valid_update_attributes.each do |updated_attribute, updated_value| resource_after_update[updated_attribute].should eql(updated_value) end end with_malformed_data_should_respond_with_400 with_non_existent_resource_should_respond_with_404 instance_eval(&additional_examples) if additional_examples end end |
#given_create_attributes(values = {}) ⇒ Object
71 72 73 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 71 def given_create_attributes(values = {}) let(:valid_create_attributes) { values } end |
#given_update_attributes(values = {}) ⇒ Object
75 76 77 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 75 def given_update_attributes(values = {}) let(:valid_update_attributes) { values } end |
#metadata_for_formatted_resource(format, options = {}) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 95 def (format, = {}) { :format => format, :collection_url => collection_url_for_resource([:namespace_path], [:parents], [:resource_name], format), :resource_url => resource_url_for_resource([:namespace_path], [:parents], [:resource_name], format), :mime_type => Mime::Type.lookup_by_extension(format).to_s }.merge() end |
#metadata_for_resources(resource_name, options = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 79 def (resource_name, = {}) if resource_name.is_a?(Array) parents = resource_name resource_name = parents.pop end resource_name = resource_name.to_s if resource_name.is_a?(Symbol) { :formats => [ :json ], :resource_name => resource_name, :model_name => resource_name.singularize, :encoding => 'utf-8', :parents => parents || [], :namespace_path => ( [:namespace] ? "/#{[:namespace]}" : nil ) }.merge() end |
#parent_path_for_resource(parents) ⇒ Object
103 104 105 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 103 def parent_path_for_resource(parents) parents.map { |p| "/#{p}/:id" }.join end |
#request_resource_action(action, options = {}, &blk) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 158 def request_resource_action(action, = {}, &blk) = { :mime_type => [:mime_type], :encoding => [:encoding], :status => STANDARD_SUCCESSFUL_STATUS_FOR[action], :collection_url => [:collection_url], :resource_url => [:resource_url] }.merge(STANDARD_REQUEST_FOR[action]).merge() request [:method], [[:end_point]], [:payload] do let(:request_url) { send([:end_point]) } expects_status([:status]) expects_content_type([:mime_type], [:encoding]) instance_eval(&blk) if blk end end |
#resource_url_for_resource(namespace, parents, resource_name, format) ⇒ Object
111 112 113 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 111 def resource_url_for_resource(namespace, parents, resource_name, format) [ namespace, parent_path_for_resource(parents), "/#{resource_name}/:id.#{format}" ].join end |
#resources(resource_name, options = {}, &blk) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 115 def resources(resource_name, = {}, &blk) = (resource_name, ) _resource_name = [:resource_name].singularize # If you want xml, pass it as formats: [ :json, :xml ] [:formats].each do |format| = (format, ) context [:collection_url], do let(:namespace) { [:namespace] } let(:resource_name) { _resource_name } let(:parent_names) { [:parents] } let(:format) { format } let([:parents].last) { model_parent } if [:parents].last instance_eval(&blk) if blk end end end |
#with_malformed_data_should_respond_with_400 ⇒ Object
249 250 251 252 253 254 255 256 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 249 def with_malformed_data_should_respond_with_400 context "with malformed #{[:format]} payload" do let(:request_raw_payload) { send("malformed_#{format}_payload") } expects_status(400) expects_content_type([:mime_type], [:encoding]) end end |
#with_non_existent_resource_should_respond_with_404 ⇒ Object
258 259 260 261 262 263 |
# File 'lib/intermodal/rspec/requests/resources.rb', line 258 def with_non_existent_resource_should_respond_with_404 context 'with non-existent resource' do let(:resource_id) { 0 } # Assumes that persisted datastore never uses id of 0 expects_status 404 end end |