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.



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_sessionObject

Clears all the sessions.



124
125
126
# File 'lib/mack/testing/helpers.rb', line 124

def clear_session
  Mack::SessionStore.expire_all
end

#cookiesObject

Returns a Hash of cookies from the response.



129
130
131
# File 'lib/mack/testing/helpers.rb', line 129

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

#in_sessionObject

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, 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.



54
55
56
57
58
59
60
61
# File 'lib/mack/testing/helpers.rb', line 54

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:



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

Removes a cookie.



139
140
141
# File 'lib/mack/testing/helpers.rb', line 139

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

#requestObject

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

#responseObject

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

#responsesObject

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

#sessionObject

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



134
135
136
# File 'lib/mack/testing/helpers.rb', line 134

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.



31
32
33
34
35
# File 'lib/mack/testing/helpers.rb', line 31

def temp_app_config(options = {})
  configatron.temp(options) do
    yield
  end
end