Best Spork_ruby code snippet using Spork.using_spork
spork.rb
Source:spork.rb  
...19    #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| p.call }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      ...spork_spec.rb
Source:spork_spec.rb  
...57  it "expands a caller line, preserving the line number" do58    Spork.send(:expanded_caller, "/boo/../yah.rb:31").should == "/yah.rb:31"59  end60  61  describe "#using_spork?" do62    it "returns true if Spork is being used" do63      Spork.using_spork?.should be_false64      Spork.using_spork!65      Spork.using_spork?.should be_true66    end67  end68  describe "#trap_method" do69    before(:each) do70      Spork.using_spork!71      72      Object.class_eval do73        class TrapTest74          def self.output75            @output ||= []76          end77          78          def hello79            TrapTest.output << 'hello'80          end81          82          def goodbye83            TrapTest.output << 'goodbye'84          end85          86          def say_something!87            TrapTest.output << 'something'88          end89        end90      end91      @trap_test = TrapTest.new92    end93    94    after(:each) do95      Object.send(:remove_const, :TrapTest)96    end97    98    it "delays execution of a method until after Spork.exec_each_run is called" do99      Spork.using_spork!100      Spork.trap_method(TrapTest, :hello)101      @trap_test.hello102      @trap_test.goodbye103      Spork.exec_each_run104      TrapTest.output.should == ['goodbye', 'hello']105    end106    107    it "works with methods that have punctuation" do108      Spork.trap_method(TrapTest, :say_something!)109      @trap_test.say_something!110      TrapTest.output.should == []111      Spork.exec_each_run112      TrapTest.output.should == ['something']113    end114  end115  116  describe "#trap_class_method" do117    before(:each) do118      Object.class_eval do119        class TrapTest120          def self.output121            @output ||= []122          end123          124          def self.hello125            output << 'hello'126          end127          128          def self.goodbye129            output << 'goodbye'130          end131        end132      end133    end134    135    after(:each) do136      Object.send(:remove_const, :TrapTest)137    end138    139    it "delays execution of a method until after Spork.exec_each_run is called" do140      Spork.using_spork!141      Spork.trap_class_method(TrapTest, :hello)142      TrapTest.hello143      TrapTest.goodbye144      Spork.exec_each_run145      TrapTest.output.should == ['goodbye', 'hello']146    end147  end148end...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!!
