Top Level Namespace
Defined Under Namespace
Modules: ActionDispatch, ActiveSupport, Admin, AdminGate, Cybertrain, Gen, I18n, ViewComponent Classes: Article, ArticleRelation, Comment, CommentComponent, CommentRelation, CybertrainJobsController, Date, Integer, Time, WelcomeJob
Instance Method Summary collapse
-
#assert(condition, message = "expected condition to be truthy")
Passes when the condition is truthy.
-
#assert_broadcasts(stream, count) { ... } ⇒ Array<String>
Passes when
countmessages were broadcast tostream(rows written tocybertrain_messages) while the block ran, or, without a block, since the test began; answers their payloads, oldest first. -
#assert_changes(expression, from: Cybertrain::Test::UNSET, to: Cybertrain::Test::UNSET) { ... } ⇒ nil
Passes when the block changes the value of
expression(compared with==), fromfrom:and toto:when they are given (to: nilis an expectation). -
#assert_difference(expression, difference = 1) { ... } ⇒ nil
Passes when the block changes the value of
expressionbydifference. -
#assert_emails(count) { ... } ⇒ nil
Passes when the block delivered
countmails: the messages Mailer.deliveries gained during it, after the mail queued in it with deliver_later was delivered (the “cybertrain mail” jobs queued in the block are performed first; jobs of other kinds stay queued). -
#assert_empty(object) ⇒ nil
Passes when a String, Array or Hash is empty.
-
#assert_enqueued_jobs(count, kind = "")
Passes when
countjobs are queued (rows in statequeued, not yet run), ofkindwhen one is given. -
#assert_enqueued_with(kind, args = Cybertrain::Cast.empty_hash)
Passes when at least one job of
kindis queued whose args contain every given key with an equal value. -
#assert_equal(expected, actual)
Passes when
expected == actual; fails with Minitest’s text, the two values’inspecton two lines (Expected: 1/ ` Actual: 0`). -
#assert_in_delta(expected, actual, delta = 0.001) ⇒ nil
Passes when
actualis withindeltaofexpected(Minitest’s default delta, 0.001). -
#assert_includes(haystack, needle)
Passes when a String contains
needle.to_sor an Array includesneedle. -
#assert_match(matcher, string) ⇒ nil
Passes when
stringmatches: a Regexp is matched, a String is looked for as a substring. -
#assert_nil(actual)
Passes when
actualis nil. -
#assert_no_broadcasts(stream) { ... } ⇒ nil
Passes when nothing was broadcast to
streamwhile the block ran, or, without a block, since the test began:assert_broadcasts(stream, 0)(and, as it, blind to rows a real server’s poller pruned). -
#assert_no_difference(expression) { ... } ⇒ nil
Passes when the block leaves the value of
expressionunchanged. -
#assert_no_emails { ... } ⇒ nil
Passes when the block delivered no mail:
assert_emails(0) { }, the deliver_later mail queued in it included. -
#assert_no_enqueued_jobs(kind = "")
Passes when no job is queued, of
kindwhen one is given. -
#assert_no_match(matcher, string) ⇒ nil
Passes when
stringdoes not match (see #assert_match). -
#assert_not(object) ⇒ nil
Passes when
objectis false or nil (Rails’ name forrefute). -
#assert_not_empty(object) ⇒ nil
Passes when a String, Array or Hash is not empty.
-
#assert_not_equal(expected, actual) ⇒ nil
Passes when
expected != actual. -
#assert_not_includes(collection, object) ⇒ nil
Passes when a String does not contain
object.to_s, or an Array does not includeobject(the opposite ofassert_includes). -
#assert_not_nil(object) ⇒ nil
Passes when
objectis not nil. -
#assert_nothing_raised { ... } ⇒ nil
Passes when the block raises nothing; otherwise fails naming the exception’s class and message.
-
#assert_raises(expected = "") { ... } ⇒ String
Passes when the block raises the exception
expectednames, and answers that exception’s message, a String. -
#assert_redirected_to(response, path)
Passes when the response is a 3xx whose Location is
path; an absolute URL on the application’s host (www.example.com, or Cybertrain.url_root’s, as the*_urlhelpers write) counts as its path, on either side. -
#assert_response(response, expected)
assert_response res, :ok / :not_found / 201 …; :redirect accepts any 3xx.
-
#assert_select(selector, equality = nil, count: nil, minimum: nil, maximum: nil, text: nil) ⇒ Array<Cybertrain::Test::HtmlNode>
Checks the last rendered HTML (
render_inline’s, or the last response of an integration test) for elements matchingselector, and answers them. -
#assert_turbo_stream(action:, target:, count: 1) ⇒ nil
Passes when the last response (or
render_inline) of this test holds exactlycount<turbo-stream action="..." target="...">elements, as turbo-rails’ assertion does (count 1 by default). -
#clear_enqueued_jobs ⇒ Integer
Deletes every queued job, so the next test starts from an empty queue.
-
#flunk(message)
Fails the current case.
-
#freeze_time { ... } ⇒ nil
travel_to(Time.current): time stops where it is. -
#perform_enqueued_jobs(now = Time.current) ⇒ Integer
Without a block: runs every job due at
nowon the calling thread, until none is due (Job.work_one), and returns how many it ran. -
#refute(condition, message = "expected condition to be falsy")
Passes when the condition is false or nil.
-
#render_inline(component = nil, content: "", partial: "", locals: {}) ⇒ String
Renders a component (
render_inline(CardComponent.new(title: "x")), its content ascontent:) or a partial (render_inline(partial: "articles/form", locals: { article: Article.new })) outside any request, answers the HTML and keeps it forassert_select(spec section 9.1). -
#setup(&block) ⇒ nil
Registers a block run before every case of this file, after the reset hooks; several run in the order written.
-
#teardown(&block) ⇒ nil
Registers a block run after every case of this file, whatever happened in it; several run in the reverse of the order written.
-
#test(name, &block)
Registers a test case, run in order by Cybertrain::Test.run!.
-
#travel(seconds) { ... } ⇒ nil
travel_to(Time.current + seconds). -
#travel_back ⇒ nil
Returns the clock to real time.
-
#travel_to(time) { ... } ⇒ nil
Sets the clock to
timeand holds it there.
Instance Method Details
#assert(condition, message = "expected condition to be truthy")
This method returns an undefined value.
Passes when the condition is truthy.
659 660 661 662 |
# File 'cybertrain/test.rb', line 659 def assert(condition, = "expected condition to be truthy") Cybertrain::Test.count_assertion flunk() unless condition end |
#assert_broadcasts(stream, count) { ... } ⇒ Array<String>
Passes when count messages were broadcast to stream (rows written to
cybertrain_messages) while the block ran, or, without a block, since
the test began; answers their payloads, oldest first. stream is a name
or a record (its Cybertrain::Stream.name_for, "articles:12").
Only rows still in the table are counted. In-process nothing deletes
them, but a test that boots a real server (Application#serve) runs its
poller, which prunes rows older than Config#stream_retention by the
Clock: after travel_to far ahead, rows broadcast before the travel can
be gone, so a count without a block comes out low and
assert_no_broadcasts passes. Count before travelling, or wrap the
broadcasting code in the block.
347 348 349 350 351 352 353 354 355 356 357 |
# File 'cybertrain/test/broadcasts.rb', line 347 def assert_broadcasts(stream, count) name = Cybertrain::Stream.name_for(stream) floor = Cybertrain::Test. if block_given? floor = Cybertrain::Test::Broadcasts.head_id yield end payloads = Cybertrain::Test::Broadcasts.payloads_after(name, floor) Cybertrain::Test.check_broadcasts(name, count, payloads.length) payloads end |
#assert_changes(expression, from: Cybertrain::Test::UNSET, to: Cybertrain::Test::UNSET) { ... } ⇒ nil
Passes when the block changes the value of expression (compared with
==), from from: and to to: when they are given (to: nil is an
expectation). Checked in Rails’ order: from:, then the change, then
to:.
470 471 472 473 474 475 |
# File 'cybertrain/test/assertions.rb', line 470 def assert_changes(expression, from: Cybertrain::Test::UNSET, to: Cybertrain::Test::UNSET) before = expression.call yield after = expression.call Cybertrain::Test.check_changes(before, after, from, to) end |
#assert_difference(expression, difference = 1) { ... } ⇒ nil
Passes when the block changes the value of expression by
difference. The expression is a lambda, passed positionally (Rails’
String expression needs eval).
435 436 437 438 439 440 |
# File 'cybertrain/test/assertions.rb', line 435 def assert_difference(expression, difference = 1) before = expression.call yield after = expression.call Cybertrain::Test.check_difference(before, after, difference) end |
#assert_emails(count) { ... } ⇒ nil
Passes when the block delivered count mails: the messages
Mailer.deliveries gained during it, after the mail queued in it with
deliver_later was delivered (the “cybertrain mail” jobs queued in the
block are performed first; jobs of other kinds stay queued). A mail job
that fails (no sender, a refused address) fails the assertion with its
error rather than counting short: assert_emails: the job "cybertrain
mail" (id 2) raised ArgumentError: the from address is empty (now
discarded). Without a
block, passes when Mailer.deliveries holds count messages (it is
emptied before every test).
72 73 74 75 76 77 78 79 80 81 |
# File 'cybertrain/test/mail.rb', line 72 def assert_emails(count) before = 0 if block_given? before = Cybertrain::Mailer.deliveries.size floor = Cybertrain::Test::Mail.job_floor yield Cybertrain::Test::Mail.deliver_queued(floor, "assert_emails") end Cybertrain::Test.check_emails(count, Cybertrain::Mailer.deliveries.size - before) end |
#assert_empty(object) ⇒ nil
Passes when a String, Array or Hash is empty.
383 384 385 |
# File 'cybertrain/test/assertions.rb', line 383 def assert_empty(object) Cybertrain::Test.check_empty(object, true) end |
#assert_enqueued_jobs(count, kind = "")
This method returns an undefined value.
Passes when count jobs are queued (rows in state queued, not yet
run), of kind when one is given.
142 143 144 145 146 147 148 149 |
# File 'cybertrain/test/jobs.rb', line 142 def assert_enqueued_jobs(count, kind = "") Cybertrain::Test.count_assertion got = Cybertrain::Test::Jobs.queued_count(kind) return if got == count what = kind.empty? ? "jobs" : "#{kind.inspect} jobs" flunk("expected #{count} enqueued #{what}, got #{got}") end |
#assert_enqueued_with(kind, args = Cybertrain::Cast.empty_hash)
This method returns an undefined value.
Passes when at least one job of kind is queued whose args contain every
given key with an equal value. Both sides are compared as Cast.str, so
"user_id" => 1 matches a JSON 1 (and “1”). A Symbol key is read as its
name, so user_id: 1 works too.
177 178 179 180 181 182 183 184 185 186 187 |
# File 'cybertrain/test/jobs.rb', line 177 def assert_enqueued_with(kind, args = Cybertrain::Cast.empty_hash) Cybertrain::Test.count_assertion payloads = Cybertrain::Test::Jobs.queued_payloads(kind) payloads.each do |payload| return if Cybertrain::Test::Jobs.args_match?(payload, args) end wanted = args.empty? ? "" : " with #{Cybertrain::Test::Jobs.describe_args(args)}" queued = payloads.empty? ? "none queued" : "queued: #{payloads.join(", ")}" flunk("expected an enqueued #{kind.inspect} job#{wanted}; #{queued}") end |
#assert_equal(expected, actual)
This method returns an undefined value.
Passes when expected == actual; fails with Minitest’s text, the two
values’ inspect on two lines (Expected: 1 / ` Actual: 0`).
680 681 682 |
# File 'cybertrain/test.rb', line 680 def assert_equal(expected, actual) Cybertrain::Test.check_equal(expected, actual) end |
#assert_in_delta(expected, actual, delta = 0.001) ⇒ nil
Passes when actual is within delta of expected (Minitest’s default
delta, 0.001).
327 328 329 |
# File 'cybertrain/test/assertions.rb', line 327 def assert_in_delta(expected, actual, delta = 0.001) Cybertrain::Test.check_in_delta(expected, actual, delta) 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.
707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
# File 'cybertrain/test.rb', line 707 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_match(matcher, string) ⇒ nil
Passes when string matches: a Regexp is matched, a String is looked
for as a substring.
339 340 341 |
# File 'cybertrain/test/assertions.rb', line 339 def assert_match(matcher, string) Cybertrain::Test.check_match(matcher, string, true) end |
#assert_nil(actual)
This method returns an undefined value.
Passes when actual is nil.
688 689 690 691 |
# File 'cybertrain/test.rb', line 688 def assert_nil(actual) Cybertrain::Test.count_assertion flunk("expected nil, got #{actual.inspect}") unless actual.nil? end |
#assert_no_broadcasts(stream) { ... } ⇒ nil
Passes when nothing was broadcast to stream while the block ran, or,
without a block, since the test began: assert_broadcasts(stream, 0)
(and, as it, blind to rows a real server’s poller pruned).
366 367 368 369 370 371 372 373 374 375 |
# File 'cybertrain/test/broadcasts.rb', line 366 def assert_no_broadcasts(stream) name = Cybertrain::Stream.name_for(stream) floor = Cybertrain::Test. if block_given? floor = Cybertrain::Test::Broadcasts.head_id yield end got = Cybertrain::Test::Broadcasts.payloads_after(name, floor).length Cybertrain::Test.check_broadcasts(name, 0, got) end |
#assert_no_difference(expression) { ... } ⇒ nil
Passes when the block leaves the value of expression unchanged.
448 449 450 451 452 453 |
# File 'cybertrain/test/assertions.rb', line 448 def assert_no_difference(expression) before = expression.call yield after = expression.call Cybertrain::Test.check_difference(before, after, 0) end |
#assert_no_emails { ... } ⇒ nil
Passes when the block delivered no mail: assert_emails(0) { }, the
deliver_later mail queued in it included. Without a block, passes when
Mailer.deliveries is empty.
92 93 94 95 96 97 98 99 100 101 |
# File 'cybertrain/test/mail.rb', line 92 def assert_no_emails before = 0 if block_given? before = Cybertrain::Mailer.deliveries.size floor = Cybertrain::Test::Mail.job_floor yield Cybertrain::Test::Mail.deliver_queued(floor, "assert_no_emails") end Cybertrain::Test.check_emails(0, Cybertrain::Mailer.deliveries.size - before) end |
#assert_no_enqueued_jobs(kind = "")
This method returns an undefined value.
Passes when no job is queued, of kind when one is given.
157 158 159 160 161 162 163 164 |
# File 'cybertrain/test/jobs.rb', line 157 def assert_no_enqueued_jobs(kind = "") Cybertrain::Test.count_assertion got = Cybertrain::Test::Jobs.queued_count(kind) return if got == 0 what = kind.empty? ? "jobs" : "#{kind.inspect} jobs" flunk("expected no enqueued #{what}, got #{got}") end |
#assert_no_match(matcher, string) ⇒ nil
Passes when string does not match (see #assert_match).
348 349 350 |
# File 'cybertrain/test/assertions.rb', line 348 def assert_no_match(matcher, string) Cybertrain::Test.check_match(matcher, string, false) end |
#assert_not(object) ⇒ nil
Passes when object is false or nil (Rails’ name for refute).
399 400 401 |
# File 'cybertrain/test/assertions.rb', line 399 def assert_not(object) Cybertrain::Test.check_not(object) end |
#assert_not_empty(object) ⇒ nil
Passes when a String, Array or Hash is not empty.
391 392 393 |
# File 'cybertrain/test/assertions.rb', line 391 def assert_not_empty(object) Cybertrain::Test.check_empty(object, false) end |
#assert_not_equal(expected, actual) ⇒ nil
Passes when expected != actual.
357 358 359 |
# File 'cybertrain/test/assertions.rb', line 357 def assert_not_equal(expected, actual) Cybertrain::Test.check_not_equal(expected, actual) end |
#assert_not_includes(collection, object) ⇒ nil
Passes when a String does not contain object.to_s, or an Array does
not include object (the opposite of assert_includes).
375 376 377 |
# File 'cybertrain/test/assertions.rb', line 375 def assert_not_includes(collection, object) Cybertrain::Test.check_not_includes(collection, object) end |
#assert_not_nil(object) ⇒ nil
Passes when object is not nil.
365 366 367 |
# File 'cybertrain/test/assertions.rb', line 365 def assert_not_nil(object) Cybertrain::Test.check_not_nil(object) end |
#assert_nothing_raised { ... } ⇒ nil
Passes when the block raises nothing; otherwise fails naming the exception’s class and message. A failed assertion in the block fails with its own message.
412 413 414 415 416 417 418 419 420 |
# File 'cybertrain/test/assertions.rb', line 412 def assert_nothing_raised begin yield Cybertrain::Test.count_nothing_raised rescue StandardError => e Cybertrain::Test.check_nothing_raised(e) end nil end |
#assert_raises(expected = "") { ... } ⇒ String
Passes when the block raises the exception expected names, and answers
that exception’s message, a String. A Class matches that class or a
subclass, as Minitest’s does (assert_raises(ArgumentError),
assert_raises(NotImplementedError), assert_raises(Cybertrain::DB::Error));
a String matches a class whose name contains it ("RecordNotFound");
nothing given matches any StandardError. The failures are Minitest’s:
[ArgumentError] exception expected, not with the raised class and
message on the next two lines, or ArgumentError expected but nothing was
raised.
Unlike Rails (Minitest answers the exception), the answer is already the
message: where Rails writes error = assert_raises(X) { ... } and then
error.message, write message = assert_raises(X) { ... } and use
message itself.
An exception that is not a StandardError (SystemExit, Interrupt,
NoMemoryError, …) and is not the one expected is raised on untouched.
A failed assertion inside the block is not “the expected exception”:
AssertionFailed is a StandardError, so it is raised on (the test fails
with that assertion’s message) unless expected names AssertionFailed,
which is how the assertion helpers themselves are tested
(assert_raises("AssertionFailed") { ... }).
Ruby’s own exception classes answer no superclass under Spinel, so a
subclass is recognised above them from a table of their parents (the
Errno family, JSON’s, Encoding’s, URI’s and the core classes); a
built-in class outside it fails with its name, asking for the
exact class or the name form (assert_raises("Name")).
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 |
# File 'cybertrain/test.rb', line 757 def assert_raises(expected = "") Cybertrain::Test.count_assertion = "" raised = false begin yield rescue Exception => e # Exception, so NotImplementedError and other non-StandardErrors can be # expected; the helper raises on what was not. One clause only (a # second `=> e` of another type in this yielding method is untested # under Spinel, NOTES rule 32); the class is told # apart by name in the helper, which reads `name.to_s` (Class#name is # nil for an anonymous class under CRuby, and a NoMethodError from # here would replace the exception being asserted on). Cybertrain::Test.raise_unless_expected(expected, e) raised = true = e. end Cybertrain::Test.check_raised(expected, raised) end |
#assert_redirected_to(response, path)
This method returns an undefined value.
Passes when the response is a 3xx whose Location is path; an absolute
URL on the application’s host (www.example.com, or Cybertrain.url_root’s,
as the *_url helpers write) counts as its path, on either side.
837 838 839 |
# File 'cybertrain/test/client.rb', line 837 def assert_redirected_to(response, path) Cybertrain::Test.check_redirect(response, path) end |
#assert_response(response, expected)
This method returns an undefined value.
assert_response res, :ok / :not_found / 201 …; :redirect accepts any 3xx.
Symbols: every one Controller::STATUS_SYMBOLS accepts (:ok,
:created, :accepted, :no_content, :see_other, :bad_request,
:unauthorized, :forbidden, :not_found, :conflict,
:payload_too_large, :unsupported_media_type, :unprocessable_entity,
:too_many_requests, :service_unavailable, …), each standing for its
one code, plus :success (2xx), :redirect (3xx), :missing (404)
and :error (exactly 500; Rails takes any 5xx). Needs cybertrain/test/client.
824 825 826 |
# File 'cybertrain/test/client.rb', line 824 def assert_response(response, expected) Cybertrain::Test.check_response(response, expected) end |
#assert_select(selector, equality = nil, count: nil, minimum: nil, maximum: nil, text: nil) ⇒ Array<Cybertrain::Test::HtmlNode>
Checks the last rendered HTML (render_inline’s, or the last response
of an integration test) for elements matching selector, and answers
them. With no option at least one must match. The second argument may be
Rails’ positional form: a String or Regexp (the text), an Integer (the
count), a Range (2..3: at least 2, at most 3), true (at least one) or
false (none). text: is a String,
compared with each element’s text after collapsing whitespace and
stripping, or a Regexp matched against that text; only the elements
whose text matches are counted and answered.
The nested block form (assert_select "ul" do ... end) is not supported;
cybertrain test refuses it at gen, and here it fails.
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'cybertrain/test/select.rb', line 527 def assert_select(selector, equality = nil, count: nil, minimum: nil, maximum: nil, text: nil) if block_given? = "assert_select: the nested block form is not supported (selector: #{selector.inspect})" Cybertrain::Test.fail_with() end want_text = text want_count = Cybertrain::Test.select_bound("count", count) want_min = Cybertrain::Test.select_bound("minimum", minimum) want_max = Cybertrain::Test.select_bound("maximum", maximum) if want_min >= 0 && want_max >= 0 && want_min > want_max = "assert_select: minimum: #{want_min} is above maximum: #{want_max}" raise ArgumentError, end # Rails takes the count positionally or as options, not both. counted = want_count >= 0 || want_min >= 0 || want_max >= 0 case equality when nil then nil when String, Regexp unless text.nil? = "assert_select: give the text once, as the second argument or as text:" raise ArgumentError, end want_text = equality when Integer, true, false if counted = "assert_select: give the count once, as the second argument (#{equality.inspect}) or as count:, minimum: and maximum:" raise ArgumentError, end case equality when Integer then want_count = Cybertrain::Test.select_bound("count", equality) when true then want_min = 1 else want_count = 0 end when Range if counted = "assert_select: give the count once, as the second argument (#{equality.inspect}) or as count:, minimum: and maximum:" raise ArgumentError, end # Rails' `assert_select "li", 2..3`: at least the first, at most the # last (one less for `...`). if equality.begin.nil? || equality.end.nil? = "assert_select: a Range count needs both ends (got #{equality.inspect})" raise ArgumentError, end want_min = Cybertrain::Test.select_bound("count", equality.first) high = equality.last.to_i high -= 1 if equality.exclude_end? want_max = Cybertrain::Test.select_bound("count", high) if want_min > want_max = "assert_select: minimum: #{want_min} is above maximum: #{want_max}" raise ArgumentError, end else = "assert_select: the second argument must be a String, a Regexp, an Integer, a Range, true or false" raise ArgumentError, end # An unsupported selector is named before anything else. Cybertrain::Test::Selector.parse(selector) Cybertrain::Test.check_rendered html = Cybertrain::Test.last_html nodes = Cybertrain::Test.select_nodes(selector, html) label = "" found = Array.new(0) { Cybertrain::Test::HtmlNode.new("", 0, -1, 0) } case want_text when nil found = nodes when Regexp label = want_text.inspect nodes.each do |node| seen = Cybertrain::Test.squished_text(node.node_text) Cybertrain::Test.check_valid_text(node, seen) found << node if seen.match?(want_text) end else wanted = want_text.to_s label = wanted.inspect nodes.each { |node| found << node if Cybertrain::Test.squished_text(node.node_text) == wanted } end Cybertrain::Test.check_select(selector, found.length, want_count, want_min, want_max, label, html) found end |
#assert_turbo_stream(action:, target:, count: 1) ⇒ nil
Passes when the last response (or render_inline) of this test holds
exactly count <turbo-stream action="..." target="..."> elements, as
turbo-rails’ assertion does (count 1 by default). target is a DOM id
or a record (its dom_id, "article_12"); "" matches a stream with no
target (refresh). After an integration request the response must be
text/vnd.turbo-stream.html (turbo-rails’ media type check; its
status: :ok check is not made: assert the status with
assert_response). With nothing requested or rendered in this test it
fails (“No response yet”). The content of the streams’ templates is not
read; turbo-rails’ block form and targets: are not offered.
397 398 399 400 401 402 |
# File 'cybertrain/test/broadcasts.rb', line 397 def assert_turbo_stream(action:, target:, count: 1) id = Cybertrain::Turbo.target_text(target) Cybertrain::Test.check_turbo_source streams = Cybertrain::Test.turbo_stream_nodes(Cybertrain::Test.last_html) Cybertrain::Test.check_turbo_stream(action.to_s, id, count, streams) end |
#clear_enqueued_jobs ⇒ Integer
Deletes every queued job, so the next test starts from an empty queue.
237 238 239 240 241 242 243 244 245 |
# File 'cybertrain/test/jobs.rb', line 237 def clear_enqueued_jobs Cybertrain::Job.ensure_installed removed = 0 Cybertrain::DB.with do |c| c.execute("DELETE FROM cybertrain_jobs WHERE state = 'queued'") removed = c.changes end removed end |
#flunk(message)
This method returns an undefined value.
Fails the current case.
650 651 652 |
# File 'cybertrain/test.rb', line 650 def flunk() raise Cybertrain::Test::AssertionFailed, end |
#freeze_time { ... } ⇒ nil
travel_to(Time.current): time stops where it is.
57 58 59 60 61 62 63 64 65 |
# File 'cybertrain/test/time_helpers.rb', line 57 def freeze_time target = Time.current if block_given? Cybertrain::Clock.travel_during(target) { yield } else Cybertrain::Clock.travel_to(target) end nil end |
#perform_enqueued_jobs(now = Time.current) ⇒ Integer
Without a block: runs every job due at now on the calling thread,
until none is due (Job.work_one), and returns how many it ran. A retry
the job scheduled for later is left queued; pass a later now to run it.
With a block (Rails’ inline form): runs the block, then performs on the
calling thread every job queued during it, and the jobs those queue,
whatever their run_at, in enqueue order (Job.work_one_after), and
returns how many distinct jobs it ran; now is not used. Jobs queued
before the block stay queued. As Rails’ inline jobs raise into the test,
a job whose attempt ends failed or rescheduled (its handler raised), or
a mail the framework discarded (no sender, a refused address), fails the
test at once with its kind and error: perform_enqueued_jobs: the job
"BoomJob" (id 3) raised RuntimeError: boom (now queued). It is not
retried in the block. A job the application discards on purpose
(discard_on) counts as performed, as in Rails.
Either form fails the test after 10,000 jobs: a job that enqueues itself would otherwise never let the test end.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'cybertrain/test/jobs.rb', line 216 def perform_enqueued_jobs(now = Time.current) if block_given? floor = Cybertrain::Test::Jobs.max_id yield return Cybertrain::Test::Jobs.perform_after(floor, "", "perform_enqueued_jobs") end count = 0 at = now.to_i while Cybertrain::Job.work_one(at) count += 1 if count >= Cybertrain::Test::Jobs::PERFORM_LIMIT flunk("perform_enqueued_jobs ran 10000 jobs; a job re-enqueues itself due at once") end end count end |
#refute(condition, message = "expected condition to be falsy")
This method returns an undefined value.
Passes when the condition is false or nil.
669 670 671 672 |
# File 'cybertrain/test.rb', line 669 def refute(condition, = "expected condition to be falsy") Cybertrain::Test.count_assertion flunk() if condition end |
#render_inline(component = nil, content: "", partial: "", locals: {}) ⇒ String
Renders a component (render_inline(CardComponent.new(title: "x")), its
content as content:) or a partial (render_inline(partial:
"articles/form", locals: { article: Article.new })) outside any request,
answers the HTML and keeps it for assert_select (spec section 9.1).
Rails answers a Nokogiri fragment and takes the content as a block; here
the answer is the String and a block is refused. Request-bound helpers
raise Views::OutsideRequest, as in Views.render_partial.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'cybertrain/test/views.rb', line 72 def render_inline(component = nil, content: "", partial: "", locals: {}) if block_given? = "render_inline: pass the content as content:, not a block" raise ArgumentError, end if component.nil? == (partial == "") = "render_inline: pass a component or partial:, not #{component.nil? ? "neither" : "both"}" raise ArgumentError, end html = "" if partial == "" unless locals.empty? = "render_inline: locals: are for a partial; a component takes its values in new(...)" raise ArgumentError, end html = Cybertrain::Views.render_component(component, content).to_s else unless content == "" = "render_inline: content: is for a component; a partial takes locals:" raise ArgumentError, end html = Cybertrain::Views.render_partial(partial, locals).to_s end Cybertrain::Test.last_html = html html end |
#setup(&block) ⇒ nil
Registers a block run before every case of this file, after the reset
hooks; several run in the order written. In a test class (the class
form) setup do becomes the class’s def setup instead.
629 630 631 |
# File 'cybertrain/test.rb', line 629 def setup(&block) Cybertrain::Test.add_setup(block) end |
#teardown(&block) ⇒ nil
Registers a block run after every case of this file, whatever happened in it; several run in the reverse of the order written.
641 642 643 |
# File 'cybertrain/test.rb', line 641 def teardown(&block) Cybertrain::Test.add_teardown(block) end |
#test(name, &block)
This method returns an undefined value.
Registers a test case, run in order by Cybertrain::Test.run!.
616 617 618 |
# File 'cybertrain/test.rb', line 616 def test(name, &block) Cybertrain::Test.register(name, block) end |
#travel(seconds) { ... } ⇒ nil
travel_to(Time.current + seconds).
43 44 45 46 47 48 49 50 51 |
# File 'cybertrain/test/time_helpers.rb', line 43 def travel(seconds) target = Time.current + seconds if block_given? Cybertrain::Clock.travel_during(target) { yield } else Cybertrain::Clock.travel_to(target) end nil end |
#travel_back ⇒ nil
Returns the clock to real time.
70 71 72 |
# File 'cybertrain/test/time_helpers.rb', line 70 def travel_back Cybertrain::Clock.travel_back end |
#travel_to(time) { ... } ⇒ nil
Sets the clock to time and holds it there.
26 27 28 29 30 31 32 33 |
# File 'cybertrain/test/time_helpers.rb', line 26 def travel_to(time) if block_given? Cybertrain::Clock.travel_during(time) { yield } else Cybertrain::Clock.travel_to(time) end nil end |