Top Level Namespace

Defined Under Namespace

Modules: Cybertrain, Gen Classes: Article, ArticleRelation, Comment, CommentRelation

Instance Method Summary collapse

Instance Method Details

#assert(condition, message = "expected condition to be truthy")

This method returns an undefined value.

Passes when the condition is truthy.

Parameters:

  • condition (Object)
  • message (String) (defaults to: "expected condition to be truthy") —

    the failure message



112
113
114
115
# File 'cybertrain/test.rb', line 112

def assert(condition, message = "expected condition to be truthy")
  Cybertrain::Test.count_assertion
  flunk(message) unless condition
end

#assert_equal(expected, actual)

This method returns an undefined value.

Passes when expected == actual; fails with expected X, got Y.

Parameters:

  • expected (Object)
  • actual (Object)


132
133
134
135
136
137
# File 'cybertrain/test.rb', line 132

def assert_equal(expected, actual)
  Cybertrain::Test.count_assertion
  unless expected == actual
    flunk("expected #{expected.inspect}, got #{actual.inspect}")
  end
end

#assert_includes(haystack, needle)

This method returns an undefined value.

Passes when a String contains needle.to_s or an Array includes needle. Any other haystack (a Hash) fails.

include? on a polymorphic receiver mis-dispatches under Spinel 2026.09.12 when the argument is also polymorphic (and String#include? does so as soon as SafeString is in the program), so the receiver is narrowed first and the String case goes through #index.

Examples:

assert_includes article.errors.full_messages, "Title can't be blank"
assert_includes client.response.body, "Hello Rails"

Parameters:

  • haystack (String, Array)
  • needle (Object)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'cybertrain/test.rb', line 162

def assert_includes(haystack, needle)
  Cybertrain::Test.count_assertion
  found = false
  case haystack
  when String
    n = needle.to_s
    found = !haystack.index(n).nil?
  when Array
    found = haystack.include?(needle)
  end
  unless found
    flunk("expected #{haystack.inspect} to include #{needle.inspect}")
  end
end

#assert_nil(actual)

This method returns an undefined value.

Passes when actual is nil.

Parameters:

  • actual (Object)


143
144
145
146
# File 'cybertrain/test.rb', line 143

def assert_nil(actual)
  Cybertrain::Test.count_assertion
  flunk("expected nil, got #{actual.inspect}") unless actual.nil?
end

#assert_raises(class_name = "") { ... } ⇒ String

Passes when the block raises. class_name (optional) must be a substring of the raised exception’s class name; the raised message is returned so callers can assert on it. A failing assertion inside the block is not “the expected exception”: AssertionFailed is a StandardError, so without its own clause (first, it is the more specific class) the rescue below would swallow it and the test would pass vacuously. It is re-raised so the enclosing test fails, unless the caller names it (assert_raises("AssertionFailed") { ... } is how the assertion helpers themselves are tested).

Examples:

message = assert_raises("RecordNotFound") { Article.find(999) }
assert_includes message, "id=999"

Parameters:

  • class_name (String) (defaults to: "") —

    part of the expected exception’s class name ("RecordNotFound"); the class itself cannot be given

Yields:

  • the code that must raise a StandardError

Returns:

  • (String) —

    the exception’s message



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'cybertrain/test.rb', line 193

def assert_raises(class_name = "")
  Cybertrain::Test.count_assertion
  message = ""
  raised = false
  begin
    yield
  rescue StandardError => e
    # One clause only (a second `=> e` of another type in this yielding
    # method is untested under Spinel, NOTES rule 32). `to_s`: Class#name is
    # nil for an anonymous class under CRuby (`Class.new(StandardError)`), and
    # a NoMethodError from inside this rescue clause would replace the very
    # exception being asserted on.
    name = e.class.name.to_s
    if name.include?("AssertionFailed") && !(class_name != "" && name.include?(class_name))
      raise e
    end
    raised = true
    message = e.message
    if class_name != "" && !name.include?(class_name)
      flunk("expected #{class_name} to be raised, got #{name}: #{e.message}")
    end
  end
  flunk("expected #{class_name == "" ? "an exception" : class_name} to be raised") unless raised
  message
end

#assert_redirected_to(response, path)

This method returns an undefined value.

Passes when the response is a 3xx whose Location is exactly path.

Examples:

assert_redirected_to res, "/articles/#{Article.last.id}"

Parameters:



233
234
235
236
237
238
239
# File 'cybertrain/test/client.rb', line 233

def assert_redirected_to(response, path)
  Cybertrain::Test.count_assertion
  status = response.status
  flunk("expected a redirect to #{path.inspect}, got #{status}") if status < 300 || status > 399
  location = response.header("Location")
  flunk("expected a redirect to #{path.inspect}, got #{location.inspect}") unless location == path
end

#assert_response(response, expected)

This method returns an undefined value.

assert_response res, :ok / :not_found / 201 …; :redirect accepts any 3xx.

Symbols: :ok, :success (2xx), :created, :no_content, :redirect (3xx), :see_other, :bad_request, :forbidden, :not_found, :unprocessable_entity, :error (500). Needs cybertrain/test/client.

Parameters:



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'cybertrain/test/client.rb', line 209

def assert_response(response, expected)
  Cybertrain::Test.count_assertion
  status = response.status
  case expected
  when Integer
    flunk("expected response #{expected}, got #{status}") unless status == expected
  when Symbol
    range = Cybertrain::Test::Client.status_range(expected)
    flunk("unknown status symbol #{expected.inspect}") if range[0] == 0
    unless status >= range[0] && status <= range[1]
      flunk("expected response #{expected.inspect}, got #{status}")
    end
  else
    flunk("expected an Integer or Symbol status, got #{expected.inspect}")
  end
end

#flunk(message)

This method returns an undefined value.

Fails the current case.

Parameters:

  • message (String)

Raises:



103
104
105
# File 'cybertrain/test.rb', line 103

def flunk(message)
  raise Cybertrain::Test::AssertionFailed, message
end

#refute(condition, message = "expected condition to be falsy")

This method returns an undefined value.

Passes when the condition is false or nil.

Parameters:

  • condition (Object)
  • message (String) (defaults to: "expected condition to be falsy") —

    the failure message



122
123
124
125
# File 'cybertrain/test.rb', line 122

def refute(condition, message = "expected condition to be falsy")
  Cybertrain::Test.count_assertion
  flunk(message) if condition
end

#test(name, &block)

This method returns an undefined value.

Registers a test case, run in order by Cybertrain::Test.run!.

Examples:

test "POST /articles creates an article" do
  ...
end

Parameters:

  • name (String)


94
95
96
# File 'cybertrain/test.rb', line 94

def test(name, &block)
  Cybertrain::Test.register(name, block)
end