Module: Mack::TestHelpers

Defined in:
lib/test_extensions/test_helpers.rb

Instance Method Summary collapse

Instance Method Details

#assigns(key) ⇒ Object

Retrieves an instance variable from the controller from a request.



20
21
22
# File 'lib/test_extensions/test_helpers.rb', line 20

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.



74
75
76
# File 'lib/test_extensions/test_helpers.rb', line 74

def clear_session
  Cachetastic::Caches::MackSessionCache.expire_all
end

#cookiesObject

Returns a Hash of cookies from the response.



79
80
81
# File 'lib/test_extensions/test_helpers.rb', line 79

def cookies
  test_cookies
end

#delete(uri, options = {}) ⇒ Object

Performs a ‘delete’ request for the specified uri.



40
41
42
# File 'lib/test_extensions/test_helpers.rb', line 40

def delete(uri, options = {})
  build_response(request.delete(uri, build_request_options(options)))
end

#get(uri, options = {}) ⇒ Object

Performs a ‘get’ request for the specified uri.



25
26
27
# File 'lib/test_extensions/test_helpers.rb', line 25

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.



65
66
67
68
69
70
71
# File 'lib/test_extensions/test_helpers.rb', line 65

def in_session
  @in_session = true
  clear_session
  yield
  clear_session
  @in_session = false
end

#post(uri, options = {}) ⇒ Object

Performs a ‘post’ request for the specified uri.



35
36
37
# File 'lib/test_extensions/test_helpers.rb', line 35

def post(uri, options = {})
  build_response(request.post(uri, build_request_options({:input => options.to_params})))
end

#put(uri, options = {}) ⇒ Object

Performs a ‘put’ request for the specified uri.



30
31
32
# File 'lib/test_extensions/test_helpers.rb', line 30

def put(uri, options = {})
  build_response(request.put(uri, build_request_options({:input => options.to_params})))
end

#remote_testObject



13
14
15
16
17
# File 'lib/test_extensions/test_helpers.rb', line 13

def remote_test
  if (app_config.run_remote_tests)
    yield
  end
end

Removes a cookie.



89
90
91
# File 'lib/test_extensions/test_helpers.rb', line 89

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

#requestObject

Returns a Rack::MockRequest. If there isn’t one, a new one is created.



45
46
47
# File 'lib/test_extensions/test_helpers.rb', line 45

def request
  @request ||= Rack::MockRequest.new(mack_app)
end

#responseObject

Returns the last Rack::MockResponse that got generated by a call.



50
51
52
# File 'lib/test_extensions/test_helpers.rb', line 50

def response
  responses.last
end

#responsesObject

Returns all the Rack::MockResponse objects that get generated by a call.



55
56
57
# File 'lib/test_extensions/test_helpers.rb', line 55

def responses
  @responses
end

#sessionObject

Returns a Mack::Session from the request.



60
61
62
# File 'lib/test_extensions/test_helpers.rb', line 60

def session # :nodoc:
  Cachetastic::Caches::MackSessionCache.get(cookies[app_config.mack.session_id])
end

Sets a cookie to be used for the next request



84
85
86
# File 'lib/test_extensions/test_helpers.rb', line 84

def set_cookie(name, value)
  test_cookies[name] = value
end

#temp_app_config(options = {}) ⇒ Object



7
8
9
10
11
# File 'lib/test_extensions/test_helpers.rb', line 7

def temp_app_config(options = {})
  app_config.load_hash(options, String.randomize)
  yield
  app_config.revert
end