How to use each_run_procs method of Spork Package

Best Spork_ruby code snippet using Spork.each_run_procs


Source:spork.rb Github


Full Screen

...20 # * +prevent_double_run+ - Pass false to disable double run prevention21 def each_run(prevent_double_run = true, &block)22 return if prevent_double_run && already_ran?(caller.first)23 if @state == :using_spork24 each_run_procs << block25 else26 yield27 end28 end29 30 # Used by the server. Sets the state to activate spork. Otherwise, prefork and each_run are run in passive mode, allowing specs without a Spork server.31 def using_spork!32 @state = :using_spork33 end34 35 def using_spork?36 @state == :using_spork37 end38 # Used by the server. Returns the current state of Spork.39 def state40 @state ||= :not_using_spork41 end42 43 # Used by the server. Called when loading the prefork blocks of the code.44 def exec_prefork(&block)45 using_spork!46 yield47 end48 49 # Used by the server. Called to run all of the prefork blocks.50 def exec_each_run(&block)51 each_run_procs.each { |p| }52 each_run_procs.clear53 yield if block_given?54 end55 56 # Traps an instance method of a class (or module) so any calls to it don't actually run until Spork.exec_each_run57 def trap_method(klass, method_name)58 method_name_without_spork, method_name_with_spork = alias_method_names(method_name, :spork)59 60 klass.class_eval <<-EOF, __FILE__, __LINE__ + 161 alias :#{method_name_without_spork} :#{method_name} unless method_defined?(:#{method_name_without_spork}) 62 def #{method_name}(*args)63 Spork.each_run(false) do64 #{method_name_without_spork}(*args)65 end66 end67 EOF68 end69 70 # Same as trap_method, but for class methods instead71 def trap_class_method(klass, method_name)72 trap_method((class << klass; self; end), method_name)73 end74 75 def detect_and_require(subfolder)76 ([LIBDIR] + Gem.latest_load_paths.grep(/spork/)).uniq.each do |gem_path|77 Dir.glob(File.join(gem_path, subfolder)).each { |file| require file }78 end79 end80 private81 def alias_method_names(method_name, feature)82 /^(.+?)([\?\!]{0,1})$/.match(method_name.to_s)83 ["#{$1}_without_spork#{$2}", "#{$1}_with_spork#{$2}"]84 end85 86 def already_ran87 @already_ran ||= []88 end89 90 def expanded_caller(caller_line)91 file, line = caller_line.split(":")92 line.gsub(/:.+/, '')93 File.expand_path(file, Dir.pwd) + ":" + line94 end95 96 def already_ran?(caller_script_and_line)97 return true if already_ran.include?(expanded_caller(caller_script_and_line))98 already_ran << expanded_caller(caller_script_and_line)99 false100 end101 102 def each_run_procs103 @each_run_procs ||= []104 end105 end106end...

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:


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?