Best Minitest_ruby code snippet using Reportable.nodoc
test.rb
Source:test.rb
...8 class Test < Runnable9 require "minitest/assertions"10 include Minitest::Assertions11 include Minitest::Reportable12 def class_name # :nodoc:13 self.class.name # for Minitest::Reportable14 end15 PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, SystemExit] # :nodoc:16 # :stopdoc:17 class << self; attr_accessor :io_lock; end18 self.io_lock = Mutex.new19 # :startdoc:20 ##21 # Call this at the top of your tests when you absolutely22 # positively need to have ordered tests. In doing so, you're23 # admitting that you suck and your tests are weak.24 def self.i_suck_and_my_tests_are_order_dependent!25 class << self26 undef_method :test_order if method_defined? :test_order27 define_method :test_order do :alpha end28 end29 end30 ##31 # Make diffs for this Test use #pretty_inspect so that diff32 # in assert_equal can have more details. NOTE: this is much slower33 # than the regular inspect but much more usable for complex34 # objects.35 def self.make_my_diffs_pretty!36 require "pp"37 define_method :mu_pp, &:pretty_inspect38 end39 ##40 # Call this at the top of your tests when you want to run your41 # tests in parallel. In doing so, you're admitting that you rule42 # and your tests are awesome.43 def self.parallelize_me!44 include Minitest::Parallel::Test45 extend Minitest::Parallel::Test::ClassMethods46 end47 ##48 # Returns all instance methods starting with "test_". Based on49 # #test_order, the methods are either sorted, randomized50 # (default), or run in parallel.51 def self.runnable_methods52 methods = methods_matching(/^test_/)53 case self.test_order54 when :random, :parallel then55 max = methods.size56 methods.sort.sort_by { rand max }57 when :alpha, :sorted then58 methods.sort59 else60 raise "Unknown test_order: #{self.test_order.inspect}"61 end62 end63 ##64 # Defines the order to run tests (:random by default). Override65 # this or use a convenience method to change it for your tests.66 def self.test_order67 :random68 end69 TEARDOWN_METHODS = %w[ before_teardown teardown after_teardown ] # :nodoc:70 ##71 # Runs a single test with setup/teardown hooks.72 def run73 with_info_handler do74 time_it do75 capture_exceptions do76 before_setup; setup; after_setup77 self.send self.name78 end79 TEARDOWN_METHODS.each do |hook|80 capture_exceptions do81 self.send hook82 end83 end84 end85 end86 Result.from self # per contract87 end88 ##89 # Provides before/after hooks for setup and teardown. These are90 # meant for library writers, NOT for regular test authors. See91 # #before_setup for an example.92 module LifecycleHooks93 ##94 # Runs before every test, before setup. This hook is meant for95 # libraries to extend minitest. It is not meant to be used by96 # test developers.97 #98 # As a simplistic example:99 #100 # module MyMinitestPlugin101 # def before_setup102 # super103 # # ... stuff to do before setup is run104 # end105 #106 # def after_setup107 # # ... stuff to do after setup is run108 # super109 # end110 #111 # def before_teardown112 # super113 # # ... stuff to do before teardown is run114 # end115 #116 # def after_teardown117 # # ... stuff to do after teardown is run118 # super119 # end120 # end121 #122 # class MiniTest::Test123 # include MyMinitestPlugin124 # end125 def before_setup; end126 ##127 # Runs before every test. Use this to set up before each test128 # run.129 def setup; end130 ##131 # Runs before every test, after setup. This hook is meant for132 # libraries to extend minitest. It is not meant to be used by133 # test developers.134 #135 # See #before_setup for an example.136 def after_setup; end137 ##138 # Runs after every test, before teardown. This hook is meant for139 # libraries to extend minitest. It is not meant to be used by140 # test developers.141 #142 # See #before_setup for an example.143 def before_teardown; end144 ##145 # Runs after every test. Use this to clean up after each test146 # run.147 def teardown; end148 ##149 # Runs after every test, after teardown. This hook is meant for150 # libraries to extend minitest. It is not meant to be used by151 # test developers.152 #153 # See #before_setup for an example.154 def after_teardown; end155 end # LifecycleHooks156 def capture_exceptions # :nodoc:157 yield158 rescue *PASSTHROUGH_EXCEPTIONS159 raise160 rescue Assertion => e161 self.failures << e162 rescue Exception => e163 self.failures << UnexpectedError.new(sanitize_exception e)164 end165 def sanitize_exception e # :nodoc:166 Marshal.dump e167 e168 rescue TypeError169 bt = e.backtrace170 e = RuntimeError.new "Wrapped undumpable exception for: #{e.class}: #{e.message}"171 e.set_backtrace bt172 e173 end174 def with_info_handler &block # :nodoc:175 t0 = Minitest.clock_time176 handler = lambda do177 warn "\nCurrent: %s#%s %.2fs" % [self.class, self.name, Minitest.clock_time - t0]178 end179 self.class.on_signal ::Minitest.info_signal, handler, &block180 end181 include LifecycleHooks182 include Guard183 extend Guard184 end # Test185end186require "minitest/unit" unless defined?(MiniTest) # compatibility layer only...
acts_as_reportable.rb
Source:acts_as_reportable.rb
1# ActsAsReportable2module ActiveRecord #:nodoc:3 module Acts #:nodoc:4 module Reportable #:nodoc:5 def self.included(base)6 base.extend(ClassMethods)7 end8 9 module ClassMethods10 def acts_as_reportable11 has_many :reports, :as => :reportable, :dependent => :destroy12 include ActiveRecord::Acts::Reportable::InstanceMethods13 extend ActiveRecord::Acts::Reportable::SingletonMethods14 end15 end16 17 module SingletonMethods18 def find_with_reported(options = {})...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!