Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created September 25, 2019 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/35b26e4a7c136d56555ce3da245f2630 to your computer and use it in GitHub Desktop.
Save JoshCheek/35b26e4a7c136d56555ce3da245f2630 to your computer and use it in GitHub Desktop.
Experiment that adds `.is` (like the old RSpec `.should`) to Minitest
require 'minitest/autorun'
module DotIs
# Track the current test so that we can run assertions in its context
def self.current_test=(test)
Thread.current[:dot_is_current_test] = test
end
def self.current_test
Thread.current.fetch :dot_is_current_test
end
# Override lifecycle hooks so we can access the current test
def before_setup(*)
DotIs.current_test = self
super
end
MiniTest::Test.include self
# Add `.is` to BasicObject (essentially, the old `.should` from RSpec)
refine BasicObject do
def is(expected)
DotIs.current_test.assert_equal(expected, self)
end
end
end
describe 'some tests' do
# Just b/c I think the output, ".F" is easier to understand
i_suck_and_my_tests_are_order_dependent!
# Lets us call `.is` on any object, but only from within this scope
using DotIs
it 'passes this test and knows about the assertion' do
(1+2).is 3
end
it 'fails this test and reports the failure' do
(1+2).is 4
end
end
# Just showing that the refinement means `.is` doesn't exist outside that scope
begin
(1+2).is 3
rescue NoMethodError => e
e # => #<NoMethodError: undefined method `is' for 3:Integer\nDid you mean? i>
end
# >> Run options: --seed 21958
# >>
# >> # Running:
# >>
# >> .F
# >>
# >> Failure:
# >> some tests#test_0002_fails this test and reports the failure [program.rb:26]:
# >> Expected: 4
# >> Actual: 3
# >>
# >> bin/rails test program.rb:43
# >>
# >>
# >>
# >> Finished in 0.001340s, 1492.5374 runs/s, 1492.5374 assertions/s.
# >> 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment