Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. require 'minitest/autorun'
  2.  
  3. module DotIs
  4. # Track the current test so that we can run assertions in its context
  5. def self.current_test=(test)
  6. Thread.current[:dot_is_current_test] = test
  7. end
  8.  
  9. def self.current_test
  10. Thread.current.fetch :dot_is_current_test
  11. end
  12.  
  13.  
  14. # Override lifecycle hooks so we can access the current test
  15. def before_setup(*)
  16. DotIs.current_test = self
  17. super
  18. end
  19.  
  20. MiniTest::Test.include self
  21.  
  22.  
  23. # Add `.is` to BasicObject (essentially, the old `.should` from RSpec)
  24. refine BasicObject do
  25. def is(expected)
  26. DotIs.current_test.assert_equal(expected, self)
  27. end
  28. end
  29. end
  30.  
  31.  
  32. describe 'some tests' do
  33. # Just b/c I think the output, ".F" is easier to understand
  34. i_suck_and_my_tests_are_order_dependent!
  35.  
  36. # Lets us call `.is` on any object, but only from within this scope
  37. using DotIs
  38.  
  39. it 'passes this test and knows about the assertion' do
  40. (1+2).is 3
  41. end
  42.  
  43. it 'fails this test and reports the failure' do
  44. (1+2).is 4
  45. end
  46. end
  47.  
  48. # Just showing that the refinement means `.is` doesn't exist outside that scope
  49. begin
  50. (1+2).is 3
  51. rescue NoMethodError => e
  52. e # => #<NoMethodError: undefined method `is' for 3:Integer\nDid you mean? i>
  53. end
  54.  
  55. # >> Run options: --seed 21958
  56. # >>
  57. # >> # Running:
  58. # >>
  59. # >> .F
  60. # >>
  61. # >> Failure:
  62. # >> some tests#test_0002_fails this test and reports the failure [program.rb:26]:
  63. # >> Expected: 4
  64. # >> Actual: 3
  65. # >>
  66. # >> bin/rails test program.rb:43
  67. # >>
  68. # >>
  69. # >>
  70. # >> Finished in 0.001340s, 1492.5374 runs/s, 1492.5374 assertions/s.
  71. # >> 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement