How to use after_each_run_procs method of Spork Package

Best Spork_ruby code snippet using Spork.after_each_run_procs

spork.rb

Source:spork.rb Github

copy

Full Screen

...42 #43 # * +prevent_double_run+ - Pass false to disable double run prevention44 def after_each_run(prevent_double_run = true, &block)45 return if prevent_double_run && already_ran?(caller.first)46 after_each_run_procs << block47 end48 def using_spork?49 state != :not_using_spork50 end51 def state52 @state ||= :not_using_spork53 end54 55 # Used by the server. Called when loading the prefork blocks of the code.56 def exec_prefork(&block)57 @state = :prefork58 yield59 end60 61 # Used by the server. Called to run all of the prefork blocks.62 def exec_each_run(&block)63 @state = :run64 activate_after_each_run_at_exit_hook65 each_run_procs.each { |p| p.call }66 each_run_procs.clear67 yield if block_given?68 end69 70 # Used by the server. Called to run all of the after_each_run blocks.71 def exec_after_each_run72 # processes in reverse order similar to at_exit73 while p = after_each_run_procs.pop; p.call; end74 true75 end76 # Traps an instance method of a class (or module) so any calls to it don't actually run until Spork.exec_each_run77 def trap_method(klass, method_name)78 method_name_without_spork, method_name_with_spork = alias_method_names(method_name, :spork)79 80 klass.class_eval <<-EOF, __FILE__, __LINE__ + 181 alias :#{method_name_without_spork} :#{method_name} unless method_defined?(:#{method_name_without_spork}) 82 def #{method_name}(*args, &block)83 Spork.each_run(false) do84 #{method_name_without_spork}(*args, &block)85 end86 end87 EOF88 end89 90 # Same as trap_method, but for class methods instead91 def trap_class_method(klass, method_name)92 trap_method((class << klass; self; end), method_name)93 end94 95 def detect_and_require(subfolder)96 ([LIBDIR.to_s] + other_spork_gem_load_paths).uniq.each do |gem_path|97 Dir.glob(File.join(gem_path, subfolder)).each { |file| require file }98 end99 end100 # This method is used to auto-discover peer plugins such as spork-testunit.101 def other_spork_gem_load_paths102 @other_spork_gem_load_paths ||= Spork::GemHelpers.latest_load_paths.grep(/spork/).select do |g|103 not g.match(%r{/spork-[0-9\-.]+/lib}) # don't include other versions of spork104 end105 end106 private107 def activate_after_each_run_at_exit_hook108 Kernel.module_eval do109 def at_exit(&block)110 Spork.after_each_run(false, &block)111 end112 end113 end114 def alias_method_names(method_name, feature)115 /^(.+?)([\?\!]{0,1})$/.match(method_name.to_s)116 ["#{$1}_without_spork#{$2}", "#{$1}_with_spork#{$2}"]117 end118 119 def already_ran120 @already_ran ||= []121 end122 123 def expanded_caller(caller_line)124 file, line = caller_line.split(/:(\d+)/)125 line.gsub(/:.+/, '')126 expanded = File.expand_path(file, Dir.pwd) + ":" + line127 if ENV['OS'] == 'Windows_NT' # windows128 expanded = expanded[2..-1]129 end130 expanded131 end132 133 def already_ran?(caller_script_and_line)134 return true if already_ran.include?(expanded_caller(caller_script_and_line))135 already_ran << expanded_caller(caller_script_and_line)136 false137 end138 139 def each_run_procs140 @each_run_procs ||= []141 end142 def after_each_run_procs143 @after_each_run_procs ||= []144 end145 end146end...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful