Module: Mack::Testing::Helpers

Included in:
Spec::Example::ExampleMethods
Defined in:
lib/mack/testing/helpers.rb

Instance Method Summary collapse

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_sessionObject

Clears all the sessions.



122
123
124
# File 'lib/mack/testing/helpers.rb', line 122

def clear_session
  Mack::SessionStore.expire_all
end

#cookiesObject

Returns a Hash of cookies from the response.



127
128
129
# File 'lib/mack/testing/helpers.rb', line 127

def cookies
  test_cookies
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, options = {})
  build_response(request.delete(uri, build_request_options({:input => options.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, options = {})
  build_response(request.get(uri, build_request_options({:input => options.to_params})))
end

#in_sessionObject

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, options = {})
  if options[:multipart]
    form_input = build_multipart_data(options)
    build_response(request.post(uri, build_request_options({"CONTENT_TYPE" => "multipart/form-data, boundary=Mack-boundary", "CONTENT_LENGTH" => form_input.size, :input => form_input})))
  else
    build_response(request.post(uri, build_request_options({:input => options.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, options = {})
  if options[:multipart]
    form_input = build_multipart_data(options)
    build_response(request.put(uri, build_request_options({"CONTENT_TYPE" => "multipart/form-data, boundary=Mack-boundary", "CONTENT_LENGTH" => form_input.size, :input => form_input})))
  else
    build_response(request.put(uri, build_request_options({:input => options.to_params})))
  end
end

#remote_testObject

: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

Removes a cookie.



137
138
139
# File 'lib/mack/testing/helpers.rb', line 137

def remove_cookie(name)
  test_cookies.delete(name)
end

#requestObject

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

#responseObject

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

#responsesObject

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

#sessionObject

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
    set_cookie(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

Sets a cookie to be used for the next request



132
133
134
# File 'lib/mack/testing/helpers.rb', line 132

def set_cookie(name, value)
  test_cookies[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(options = {})
  configatron.temp(options) do
    yield
  end
end