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.
42 43 44 |
# File 'lib/mack/testing/helpers.rb', line 42 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.
122 123 124 |
# File 'lib/mack/testing/helpers.rb', line 122 def clear_session Mack::SessionStore.expire_all end |
#cookies ⇒ Object
Returns a Hash of cookies from the response.
127 128 129 |
# File 'lib/mack/testing/helpers.rb', line 127 def end |
#delete(uri, options = {}) ⇒ Object
Performs a ‘delete’ request for the specified uri.
77 78 79 |
# File 'lib/mack/testing/helpers.rb', line 77 def delete(uri, = {}) build_response(request.delete(uri, ({:input => .to_params}))) end |
#file_for_upload(path) ⇒ Object
create a wrapper object for file upload testing.
47 48 49 |
# File 'lib/mack/testing/helpers.rb', line 47 def file_for_upload(path) return Mack::Testing::FileWrapper.new(path) end |
#get(uri, options = {}) ⇒ Object
Performs a ‘get’ request for the specified uri.
52 53 54 |
# File 'lib/mack/testing/helpers.rb', line 52 def get(uri, = {}) build_response(request.get(uri, ({:input => .to_params}))) end |
#in_session ⇒ Object
Used to create a ‘session’ around a block of code. This is great of ‘integration’ tests.
111 112 113 114 115 116 117 118 119 |
# File 'lib/mack/testing/helpers.rb', line 111 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.
57 58 59 60 61 62 63 64 |
# File 'lib/mack/testing/helpers.rb', line 57 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.
67 68 69 70 71 72 73 74 |
# File 'lib/mack/testing/helpers.rb', line 67 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:
35 36 37 38 39 |
# File 'lib/mack/testing/helpers.rb', line 35 def remote_test # :nodoc: if (configatron.mack.run_remote_tests) yield end end |
#remove_cookie(name) ⇒ Object
Removes a cookie.
137 138 139 |
# File 'lib/mack/testing/helpers.rb', line 137 def (name) .delete(name) end |
#request ⇒ Object
Returns a Rack::MockRequest. If there isn’t one, a new one is created.
82 83 84 |
# File 'lib/mack/testing/helpers.rb', line 82 def request @request ||= Rack::MockRequest.new(mack_app) end |
#response ⇒ Object
Returns the last Rack::MockResponse that got generated by a call.
87 88 89 |
# File 'lib/mack/testing/helpers.rb', line 87 def response @testing_response end |
#responses ⇒ Object
Returns all the Rack::MockResponse objects that get generated by a call.
92 93 94 |
# File 'lib/mack/testing/helpers.rb', line 92 def responses @responses end |
#session ⇒ Object
Returns a Mack::Session from the request.
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mack/testing/helpers.rb', line 97 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
132 133 134 |
# File 'lib/mack/testing/helpers.rb', line 132 def (name, value) [name] = value end |
#temp_app_config(options = {}) ⇒ Object
Temporarily changes the application configuration. Changes are reverted after the yield returns.
29 30 31 32 33 |
# File 'lib/mack/testing/helpers.rb', line 29 def temp_app_config( = {}) configatron.temp() do yield end end |