Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To run code before <em>each</em> test, use <code>before</code>. You're operating here in the context of an instance, possibly of a class generated implicitly by <code>describe</code>, so instance variables set in <code>before</code> are accessible in each test (e.g. inside an <code>it</code> block).</p> <p>To run code before <em>all</em> tests, simply wrap the tests in a class, a subclass of <code>MiniTest::Spec</code> or whatever; now, before the tests themselves, you can create a class or module, set class variables, call a class method, etc., and all of that will be available in all tests.</p> <p>Example:</p> <pre><code>require "minitest/autorun" class MySpec &lt; MiniTest::Spec class MyClass end def self.prepare puts "once" @@prepared = "prepared" @@count = 0 end prepare before do puts "before each test" @local_count = (@@count += 1) end describe "whatever" do it "first" do p MyClass p @@prepared p @local_count end it "second" do p MyClass p @@prepared p @local_count end end end </code></pre> <p>Here's the output, along with my comments in braces explaining what each line of the output proves:</p> <pre><code>once [this code, a class method, runs once before all tests] Run options: --seed 29618 [now the tests are about to run] # Running tests: before each test [the before block runs before each test] MySpec::MyClass [the class we created earlier is visible in each test] "prepared" [the class variable we set earlier is visible in each test] 1 [the instance variable from the before block is visible in each test] before each test [the before block runs before each test] MySpec::MyClass [the class we created earlier is visible in each test] "prepared" [the class variable we set earlier is visible in each test] 2 [the instance variable from the before block is visible each test] </code></pre> <p>(Note that I do not mean this output to imply any guarantee about the order in which tests will run.)</p> <p>Another approach is to use the existing <code>before</code> but wrap code to run only once in a class variable flag. Example:</p> <pre><code>class MySpec &lt; MiniTest::Spec @@flag = nil before do unless @@flag # do stuff here that is to be done only once @@flag = true end # do stuff here that is to be done every time end # ... tests go here end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload