How to use run method of Spork Package

Best Spork_ruby code snippet using Spork.run

spork.rb

Source:spork.rb Github

copy

Full Screen

...5 LIBDIR = Pathname.new(File.expand_path(File.dirname(__FILE__)))6 autoload :Server, (LIBDIR + 'spork/server').to_s7 autoload :TestFramework, (LIBDIR + 'spork/test_framework').to_s8 autoload :AppFramework, (LIBDIR + 'spork/app_framework').to_s9 autoload :RunStrategy, (LIBDIR + 'spork/run_strategy').to_s10 autoload :Runner, (LIBDIR + 'spork/runner').to_s11 autoload :Forker, (LIBDIR + 'spork/forker').to_s12 autoload :Diagnoser, (LIBDIR + 'spork/diagnoser').to_s13 autoload :GemHelpers, (LIBDIR + 'spork/gem_helpers').to_s14 class << self15 # Run a block, during prefork mode. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.16 #17 # == Parameters18 #19 # * +prevent_double_run+ - Pass false to disable double run prevention20 def prefork(prevent_double_run = true, &block)21 return if prevent_double_run && already_ran?(caller.first)22 yield23 end24 25 # Run a block AFTER the fork occurs. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.26 #27 # == Parameters28 #29 # * +prevent_double_run+ - Pass false to disable double run prevention30 def each_run(prevent_double_run = true, &block)31 return if prevent_double_run && already_ran?(caller.first)32 if state == :prefork33 each_run_procs << block34 else35 yield36 end37 end38 39 # Run a block after specs are run.40 #41 # == Parameters42 #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

spec_helper.rb

Source:spec_helper.rb Github

copy

Full Screen

...31 if spork?32 # Load all railties files33 Rails.application.railties.all { |r| r.eager_load! }34 end35 # Don't make pry a requirement to run the test suite36 begin37 require 'pry'38 require 'pry-doc'39 rescue LoadError; end40 require 'rspec/rails'41 require 'active_attr/rspec'42 require 'mocha/setup'43 require 'timecop'44 require 'webmock'45 require 'webmock/rspec'46 require 'vcr'47 require 'capybara/rspec'48 # require 'capybara/mechanize'49 # http://blog.remarkablelabs.com/2013/01/using-sidekiq-to-send-emails-asynchronously50 require 'sidekiq/testing/inline'51 Rails.backtrace_cleaner.remove_silencers!52 require 'database_cleaner'53 RSpec.configure do |config|54 config.mock_with :rspec55 config.treat_symbols_as_metadata_keys_with_true_values = true56 config.filter_run :focus => true57 #config.filter_run :js => true if ENV['JS'] == 'true'58 #config.filter_run :js => nil if ENV['JS'] == 'false'59 config.run_all_when_everything_filtered = true60 end61end62def each_run63 if spork?64 # Start simplecov unless skip env variable set65 # Solution from https://github.com/colszowka/simplecov/issues/42#issuecomment-444028466 start_simplecov unless ENV["SKIP_COV"]67 Rails.cache.clear68 ActiveSupport::Dependencies.clear69 # When specifying a class for a factory, using a class constant70 # will cause the model to be preloaded in prefork preventing71 # reloading, whereas using a string will not72 #73 # Factory.define :user, class: 'MyUserClass' do |f|74 # ...75 # end76 #FactoryGirl.reload77 ActionDispatch::Callbacks.after do78 unless FactoryGirl.factories.blank?79 FactoryGirl.definition_file_paths = [80 "#{Rails.root}/factories",81 "#{Rails.root}/spec/factories"82 ]83 FactoryGirl.factories.clear84 FactoryGirl.find_definitions85 end86 end87 end88 # Requires supporting ruby files with custom matchers and macros, etc,89 # in spec/support/ and its subdirectories.90 Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}91 # Clean the log file before each run92 File.open("#{Rails.root}/log/test.log", 'w') { |file| file.truncate(0) }93end94# If spork is available in the Gemfile it'll be used but we don't force it.95unless (begin; require 'spork'; rescue LoadError; nil end).nil?96 Spork.prefork do97 # Loading more in this block will cause your tests to run faster. However,98 # if you change any configuration or code from libraries loaded here, you'll99 # need to restart spork for it take effect.100 setup_environment101 end102 Spork.each_run do103 # This code will be run each time you run your specs.104 each_run105 end106else107 setup_environment108 each_run109end...

Full Screen

Full Screen

spork_spec.rb

Source:spork_spec.rb Github

copy

Full Screen

...3 def self.reset!4 @state = nil5 @using_spork = false6 @already_ran = nil7 @each_run_procs = nil8 end9end10describe Spork do11 before(:each) do12 Spork.reset!13 @ran = []14 end15 16 def spec_helper_simulator17 Spork.prefork do18 @ran << :prefork19 end20 21 Spork.each_run do22 @ran << :each_run23 end24 @ran25 end26 27 it "only runs the preload block when preforking" do28 Spork.exec_prefork { spec_helper_simulator }29 @ran.should == [:prefork]30 end31 32 it "only runs the each_run block when running" do33 Spork.exec_prefork { spec_helper_simulator }34 @ran.should == [:prefork]35 36 Spork.exec_each_run37 @ran.should == [:prefork, :each_run]38 end39 40 it "runs both blocks when Spork not activated" do41 spec_helper_simulator.should == [:prefork, :each_run]42 end43 44 it "prevents blocks from being ran twice" do45 Spork.exec_prefork { spec_helper_simulator }46 Spork.exec_each_run47 @ran.clear48 Spork.exec_prefork { spec_helper_simulator }49 Spork.exec_each_run50 @ran.should == []51 end52 53 it "runs multiple prefork and each_run blocks at different locations" do54 Spork.prefork { }55 Spork.each_run { }56 spec_helper_simulator.should == [:prefork, :each_run]57 end58 59 it "expands a caller line, preserving the line number" do60 Spork.send(:expanded_caller, "/boo/../yah.rb:31").should == "/yah.rb:31"61 end62 63 describe "#using_spork?" do64 it "returns true if Spork is being used" do65 Spork.using_spork?.should be_false66 Spork.exec_prefork { }67 Spork.using_spork?.should be_true68 end69 end70 describe "#trap_method" do71 before(:each) do72 Spork.exec_prefork { }73 74 Object.class_eval do75 class TrapTest76 def self.output77 @output ||= []78 end79 80 def hello81 TrapTest.output << 'hello'82 end83 84 def goodbye85 TrapTest.output << 'goodbye'86 end87 88 def say_something!89 TrapTest.output << 'something'90 end91 end92 end93 @trap_test = TrapTest.new94 end95 96 after(:each) do97 Object.send(:remove_const, :TrapTest)98 end99 100 it "delays execution of a method until after Spork.exec_each_run is called" do101 Spork.exec_prefork { }102 Spork.trap_method(TrapTest, :hello)103 @trap_test.hello104 @trap_test.goodbye105 Spork.exec_each_run106 TrapTest.output.should == ['goodbye', 'hello']107 end108 109 it "works with methods that have punctuation" do110 Spork.trap_method(TrapTest, :say_something!)111 @trap_test.say_something!112 TrapTest.output.should == []113 Spork.exec_each_run114 TrapTest.output.should == ['something']115 end116 end117 118 describe "#trap_class_method" do119 before(:each) do120 Object.class_eval do121 class TrapTest122 def self.output123 @output ||= []124 end125 126 def self.hello127 output << 'hello'128 end129 130 def self.goodbye131 output << 'goodbye'132 end133 end134 end135 end136 137 after(:each) do138 Object.send(:remove_const, :TrapTest)139 end140 141 it "delays execution of a method until after Spork.exec_each_run is called" do142 Spork.exec_prefork { }143 Spork.trap_class_method(TrapTest, :hello)144 TrapTest.hello145 TrapTest.goodbye146 Spork.exec_each_run147 TrapTest.output.should == ['goodbye', 'hello']148 end149 end150end...

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