Module: Mack::Testing::Helpers
- Included in:
- Spec::Example::ExampleMethods
- Defined in:
- lib/mack/testing/helpers.rb
Instance Method Summary collapse
-
#assigns(key) ⇒ Object
Retrieves an instance variable from the controller from a request.
-
#clear_session ⇒ Object
Clears all the sessions.
-
#cookies ⇒ Object
Returns a Hash of cookies from the response.
-
#delete(uri, options = {}) ⇒ Object
Performs a ‘delete’ request for the specified uri.
-
#file_for_upload(path) ⇒ Object
create a wrapper object for file upload testing.
-
#get(uri, options = {}) ⇒ Object
Performs a ‘get’ request for the specified uri.
-
#in_session ⇒ Object
Used to create a ‘session’ around a block of code.
-
#post(uri, options = {}) ⇒ Object
Performs a ‘post’ request for the specified uri.
-
#put(uri, options = {}) ⇒ Object
Performs a ‘put’ request for the specified uri.
-
#remote_test ⇒ Object
:nodoc:.
-
#remove_cookie(name) ⇒ Object
Removes a cookie.
-
#request ⇒ Object
Returns a Rack::MockRequest.
-
#response ⇒ Object
Returns the last Rack::MockResponse that got generated by a call.
-
#responses ⇒ Object
Returns all the Rack::MockResponse objects that get generated by a call.
-
#session ⇒ Object
Returns a Mack::Session from the request.
-
#set_cookie(name, value) ⇒ Object
Sets a cookie to be used for the next request.
-
#temp_app_config(options = {}) ⇒ Object
Temporarily changes the application configuration.
Instance Method Details
#assigns(key) ⇒ Object
Retrieves an instance variable from the controller from a request.
44 45 46 |
# File 'lib/mack/testing/helpers.rb', line 44 def assigns(key) $mack_app.instance_variable_get("@app").instance_variable_get("@response").instance_variable_get("@controller").instance_variable_get("@#{key}") end |
#clear_session ⇒ Object
Clears all the sessions.
124 125 126 |
# File 'lib/mack/testing/helpers.rb', line 124 def clear_session Mack::SessionStore.expire_all end |
#cookies ⇒ Object
Returns a Hash of cookies from the response.
129 130 131 |
# File 'lib/mack/testing/helpers.rb', line 129 def end |
#delete(uri, options = {}) ⇒ Object
Performs a ‘delete’ request for the specified uri.
79 80 81 |
# File 'lib/mack/testing/helpers.rb', line 79 def delete(uri, = {}) build_response(request.delete(uri, ())) end |
#file_for_upload(path) ⇒ Object
create a wrapper object for file upload testing.
64 65 66 |
# File 'lib/mack/testing/helpers.rb', line 64 def file_for_upload(path) return Mack::Testing::FileWrapper.new(path) end |
#get(uri, options = {}) ⇒ Object
Performs a ‘get’ request for the specified uri.
49 50 51 |
# File 'lib/mack/testing/helpers.rb', line 49 def get(uri, = {}) build_response(request.get(uri, ())) end |
#in_session ⇒ Object
Used to create a ‘session’ around a block of code. This is great of ‘integration’ tests.
113 114 115 116 117 118 119 120 121 |
# File 'lib/mack/testing/helpers.rb', line 113 def in_session @_mack_in_session = true clear_session $current_session_id = session.id yield $current_session_id = nil clear_session @_mack_in_session = false end |
#post(uri, options = {}) ⇒ Object
Performs a ‘post’ request for the specified uri.
69 70 71 72 73 74 75 76 |
# File 'lib/mack/testing/helpers.rb', line 69 def post(uri, = {}) if [:multipart] form_input = build_multipart_data() build_response(request.post(uri, ({"CONTENT_TYPE" => "multipart/form-data, boundary=Mack-boundary", "CONTENT_LENGTH" => form_input.size, :input => form_input}))) else build_response(request.post(uri, ({:input => .to_params}))) end end |
#put(uri, options = {}) ⇒ Object
Performs a ‘put’ request for the specified uri.
54 55 56 57 58 59 60 61 |
# File 'lib/mack/testing/helpers.rb', line 54 def put(uri, = {}) if [:multipart] form_input = build_multipart_data() build_response(request.put(uri, ({"CONTENT_TYPE" => "multipart/form-data, boundary=Mack-boundary", "CONTENT_LENGTH" => form_input.size, :input => form_input}))) else build_response(request.put(uri, ({:input => .to_params}))) end end |
#remote_test ⇒ Object
:nodoc:
37 38 39 40 41 |
# File 'lib/mack/testing/helpers.rb', line 37 def remote_test # :nodoc: if (configatron.mack.run_remote_tests) yield end end |
#remove_cookie(name) ⇒ Object
Removes a cookie.
139 140 141 |
# File 'lib/mack/testing/helpers.rb', line 139 def (name) .delete(name) end |
#request ⇒ Object
Returns a Rack::MockRequest. If there isn’t one, a new one is created.
84 85 86 |
# File 'lib/mack/testing/helpers.rb', line 84 def request @request ||= Rack::MockRequest.new(mack_app) end |
#response ⇒ Object
Returns the last Rack::MockResponse that got generated by a call.
89 90 91 |
# File 'lib/mack/testing/helpers.rb', line 89 def response @testing_response end |
#responses ⇒ Object
Returns all the Rack::MockResponse objects that get generated by a call.
94 95 96 |
# File 'lib/mack/testing/helpers.rb', line 94 def responses @responses end |
#session ⇒ Object
Returns a Mack::Session from the request.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mack/testing/helpers.rb', line 99 def session # :nodoc: sess = Mack::SessionStore.get($current_session_id, nil, nil, nil) if sess.nil? id = String.randomize(40).downcase (configatron.mack.session_id, id) sess = Mack::Session.new(id) Mack::SessionStore.store.direct_set(id, sess) $current_session_id = id sess end sess end |
#set_cookie(name, value) ⇒ Object
Sets a cookie to be used for the next request
134 135 136 |
# File 'lib/mack/testing/helpers.rb', line 134 def (name, value) [name] = value end |
#temp_app_config(options = {}) ⇒ Object
Temporarily changes the application configuration. Changes are reverted after the yield returns.
31 32 33 34 35 |
# File 'lib/mack/testing/helpers.rb', line 31 def temp_app_config( = {}) configatron.temp() do yield end end |