Module: Gin::Test::Assertions

Included in:
Helpers
Defined in:
lib/gin/test.rb

Overview

Helper assertion methods for tests. To contextualize tests to a specific app, use the automatically generated module assigned to your app’s class:

class MyCtrlTest < Test::Unit::TestCase
  include MyApp::TestHelper     # Sets App for mock requests.
  controller MyHomeController   # Sets default controller to use.

  def test_home
    get :home
    assert_response :success
  end
end

Instance Method Summary collapse

Instance Method Details

Checks that the given Cookie is set with the expected values. Options supported:

:secure

Boolean - SSL cookies only

:http_only

Boolean - HTTP only cookie

:domain

String - Domain on which the cookie is used

:expires_at

Time - Date and time of cookie expiration

:path

String - Path cookie applies to

:value

Object - The value of the cookie



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/gin/test.rb', line 186

def assert_cookie name, opts={}, msg=nil
  opts ||= {}
  cookie = response_cookies[name]

  assert cookie, msg || "Expected cookie #{name.inspect} but it doesn't exist"

  opts.each do |k,v|
    next if v == cookie[k]
    err_msg = msg || "Expected cookie #{k} to be #{v.inspect} but was #{cookie[k].inspect}"

    raise MiniTest::Assertion, err_msg.to_s
  end
end

#assert_css(path, opts = {}, msg = nil) ⇒ Object

Uses CSS selectors to check for data points in the response body.

Options supported are:

:count

Integer - Number of occurences of the data point.

:value

Object - The expected value of the data point.

If value is a Class, Range, or Regex, does a match. Use for XML or HTML.

assert_select '.address[domestic=Yes]'


155
156
157
# File 'lib/gin/test.rb', line 155

def assert_css path, opts={}, msg=nil
  assert_select path, opts.merge(:selector => :css), msg
end

#assert_data(path, opts = {}, msg = nil) ⇒ Object

Uses ruby-path to check for data points in the response body.

Options supported are:

:count

Integer - Number of occurences of the data point.

:value

Object - The expected value of the data point.

If value is a Class, Range, or Regex, does a match. Use for JSON, BSON, and PList data.

assert_select '**/address/domestic=YES/../value'


139
140
141
# File 'lib/gin/test.rb', line 139

def assert_data path, opts={}, msg=nil
  assert_select path, opts.merge(:selector => :rb_path), msg
end

#assert_layout(layout, msg = nil) ⇒ Object

Checks that a specific layout was used to render the response.



215
216
217
218
219
220
# File 'lib/gin/test.rb', line 215

def assert_layout layout, msg=nil
  path = @controller.template_path(layout, true)
  expected = @app.template_files(path).first
  assert templates.include?(expected),
    msg || "Expected layout `#{path}' in:\n #{templates.join("\n ")}"
end

#assert_redirect(url, *args) ⇒ Object

Checks that the response is a redirect to a given path or url.

assert_redirect "/path/to/thing"
assert_redirect "http://example.com"
assert_redirect 302, "/path/to/thing"

Raises:

  • (MiniTest::Assertion)


229
230
231
232
233
234
235
236
237
238
# File 'lib/gin/test.rb', line 229

def assert_redirect url, *args
  status   = args.shift if Integer === args[0]
  location = rack_response[1]['Location']

  msg = args.pop ||
    "Expected redirect to #{url.inspect} but was #{location.inspect}"

  raise MiniTest::Assertion, msg unless url == location
  assert_response(status || :redirect)
end

#assert_response(expected, msg = nil) ⇒ Object

Asserts the response status code and headers. Takes an integer (status code) or Symbol as the expected value:

:success

2XX status codes

:redirect

301-303, 307-308 status codes

:forbidden

403 status code

:unauthorized

401 status code

:not_found

404 status code



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gin/test.rb', line 31

def assert_response expected, msg=nil
  status = rack_response[0]
  case expected
  when :success
    assert((200..299).include?(status),
      msg || "Status expected to be in range 200..299 but was #{status.inspect}")
  when :redirect
    assert [301,302,303,307,308].include?(status),
      msg || "Status expected to be in range 301..303 or 307..308 but was #{status.inspect}"
  when :unauthorized
    assert 401 == status,
      msg || "Status expected to be 401 but was #{status.inspect}"
  when :forbidden
    assert 403 == status,
      msg || "Status expected to be 403 but was #{status.inspect}"
  when :not_found
    assert 404 == status,
      msg || "Status expected to be 404 but was #{status.inspect}"
  when :error
    assert((500..599).include?(status),
      msg || "Status expected to be in range 500..599 but was #{status.inspect}")
  else
    assert expected == status,
      msg || "Status expected to be #{expected.inspect} but was #{status.inspect}"
  end
end

#assert_route(verb, path, exp_ctrl, exp_action, msg = nil) ⇒ Object

Checks that the given route is valid and points to the expected controller and action.



245
246
247
248
249
250
251
252
253
# File 'lib/gin/test.rb', line 245

def assert_route verb, path, exp_ctrl, exp_action, msg=nil
  ctrl, action, = Array(app.router.resources_for(verb, path))[0]
  expected = "#{exp_ctrl}##{exp_action}"
  real     = "#{ctrl}##{action}"
  real_msg = ctrl && action ? "got #{real}" : "doesn't exist"

  assert expected == real,
    msg || "`#{verb.to_s.upcase} #{path}' should map to #{expected} but #{real_msg}"
end

#assert_select(key_or_path, opts = {}, msg = nil) ⇒ Object

Checks for data points in the response body. Looks at the response Content-Type to parse. Supports JSON, BSON, XML, PLIST, and HTML.

If value is a Class, Range, or Regex, does a match. Options supported are:

:count

Integer - Number of occurences of the data point.

:value

Object - The expected value of the data point.

:selector

Symbol - type of selector to use: :css, :xpath, or :rb_path

If value is a Class, Range, or Regex, does a match.

# Use CSS3 for HTML
assert_select '.address[domestic=Yes]'

# Use XPath for XML data
assert_select './/address[@domestic=Yes]'

# Use ruby-path for JSON, BSON, and PList
assert_select '**/address/domestic=YES/../value'


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gin/test.rb', line 81

def assert_select key_or_path, opts={}, msg=nil
  value = opts[:value]
  data = parsed_body
  val_msg = " with value #{value.inspect}" if !value.nil?
  count = 0

  selector = opts[:selector] ||
    case data
    when Array, Hash              then :rb_path
    when Nokogiri::HTML::Document then :xpath
    when Nokogiri::XML::Document  then :css
    end

  case selector
  when :rb_path
    use_lib 'path', 'ruby-path'
    data.find_data(key_or_path) do |p,k,pa|
      count += 1 if value.nil? || value === p[k]
      break unless opts[:count]
    end

  when :css
    data.css(key_or_path).each do |node|
      count += 1 if value.nil? || value === node.text
      break unless opts[:count]
    end

  when :xpath
    data.xpath(key_or_path).each do |node|
      count += 1 if value.nil? || value === node.text
      break unless opts[:count]
    end

  else
    raise "Unknown selector #{selector.inspect} for #{data.class}"
  end

  if opts[:count]
    assert opts[:count] == count,
      msg || "Expected #{opts[:count]} items matching '#{key_or_path}'#{val_msg} but found #{count}"
  else
    assert((count > 0),
      msg || "Expected at least one item matching '#{key_or_path}'#{val_msg} but found none")
  end
end

#assert_view(view, msg = nil) ⇒ Object

Checks that a rendered view name or path matches the one given.



204
205
206
207
208
209
# File 'lib/gin/test.rb', line 204

def assert_view view, msg=nil
  path = @controller.template_path(view)
  expected = @app.template_files(path).first
  assert templates.include?(expected),
    msg || "Expected view `#{path}' in:\n #{templates.join("\n ")}"
end

#assert_xpath(path, opts = {}, msg = nil) ⇒ Object

Uses XPath selectors to check for data points in the response body.

Options supported are:

:count

Integer - Number of occurences of the data point.

:value

Object - The expected value of the data point.

If value is a Class, Range, or Regex, does a match. Use for XML or HTML.

assert_select './/address[@domestic=Yes]'


171
172
173
# File 'lib/gin/test.rb', line 171

def assert_xpath path, opts={}, msg=nil
  assert_select path, opts.merge(:selector => :xpath), msg
end