Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement