Best Rr_ruby code snippet using RR.Injections.method_name
double_injection.rb
Source:double_injection.rb
...4 # A double_injection has 0 to many Double objects. Each Double5 # has Argument Expectations and Times called Expectations.6 class DoubleInjection < Injection7 extend(Module.new do8 def find_or_create(subject_class, method_name)9 instances[subject_class][method_name.to_sym] ||= begin10 new(subject_class, method_name.to_sym).bind11 end12 end13 def find_or_create_by_subject(subject, method_name)14 find_or_create(class << subject; self; end, method_name)15 end16 def find(subject_class, method_name)17 instances[subject_class] && instances[subject_class][method_name.to_sym]18 end19 def find_by_subject(subject, method_name)20 find(class << subject; self; end, method_name)21 end22 def exists?(subject_class, method_name)23 !!find(subject_class, method_name)24 end25 def exists_by_subject?(subject, method_name)26 exists?((class << subject; self; end), method_name)27 end28 def dispatch_method(subject,29 subject_class,30 method_name,31 arguments,32 keyword_arguments,33 block)34 subject_eigenclass = (class << subject; self; end)35 if (36 exists?(subject_class, method_name) &&37 ((subject_class == subject_eigenclass) || !subject.is_a?(Class))38 )39 find(subject_class, method_name.to_sym).dispatch_method(subject, arguments, keyword_arguments, block)40 else41 new(subject_class, method_name.to_sym).dispatch_original_method(subject, arguments, keyword_arguments, block)42 end43 end44 def reset45 instances.each do |subject_class, method_double_map|46 SingletonMethodAddedInjection.find(subject_class) && SingletonMethodAddedInjection.find(subject_class).reset47 method_double_map.keys.each do |method_name|48 reset_double(subject_class, method_name)49 end50 Injections::DoubleInjection.instances.delete(subject_class) if Injections::DoubleInjection.instances.has_key?(subject_class)51 end52 end53 def verify(*subjects)54 subject_classes = subjects.empty? ?55 Injections::DoubleInjection.instances.keys :56 subjects.map {|subject| class << subject; self; end}57 subject_classes.each do |subject_class|58 instances.include?(subject_class) &&59 instances[subject_class].keys.each do |method_name|60 verify_double(subject_class, method_name)61 end &&62 instances.delete(subject_class)63 end64 end65 # Verifies the DoubleInjection for the passed in subject and method_name.66 def verify_double(subject_class, method_name)67 Injections::DoubleInjection.find(subject_class, method_name).verify68 ensure69 reset_double subject_class, method_name70 end71 # Resets the DoubleInjection for the passed in subject and method_name.72 def reset_double(subject_class, method_name)73 double_injection = Injections::DoubleInjection.instances[subject_class].delete(method_name)74 double_injection.reset75 Injections::DoubleInjection.instances.delete(subject_class) if Injections::DoubleInjection.instances[subject_class].empty?76 end77 def instances78 @instances ||= HashWithObjectIdKey.new do |hash, subject_class|79 hash.set_with_object_id(subject_class, {})80 end81 end82 end)83 include ClassInstanceMethodDefined84 attr_reader :subject_class, :method_name, :doubles85 MethodArguments = Struct.new(:arguments,86 :keyword_arguments,87 :block)88 def initialize(subject_class, method_name)89 @subject_class = subject_class90 @method_name = method_name.to_sym91 @doubles = []92 @dispatch_method_delegates_to_dispatch_original_method = nil93 end94 # RR::DoubleInjection#register_double adds the passed in Double95 # into this DoubleInjection's list of Double objects.96 def register_double(double)97 @doubles << double98 end99 # RR::DoubleInjection#bind injects a method that acts as a dispatcher100 # that dispatches to the matching Double when the method101 # is called.102 def bind103 if subject_has_method_defined?(method_name)104 if subject_has_original_method?105 bind_method106 else107 bind_method_with_alias108 end109 else110 Injections::MethodMissingInjection.find_or_create(subject_class)111 Injections::SingletonMethodAddedInjection.find_or_create(subject_class)112 bind_method_that_self_destructs_and_delegates_to_method_missing113 end114 self115 end116 BoundObjects = {}117 def bind_method_that_self_destructs_and_delegates_to_method_missing118 id = BoundObjects.size119 BoundObjects[id] = subject_class120 subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)121 def #{method_name}(*args, &block)122 ::RR::Injections::DoubleInjection::BoundObjects[#{id}].class_eval do123 remove_method(:#{method_name})124 end125 method_missing(:#{method_name}, *args, &block)126 end127 ruby2_keywords(:#{method_name}) if respond_to?(:ruby2_keywords, true)128 RUBY129 self130 end131 def bind_method132 id = BoundObjects.size133 BoundObjects[id] = subject_class134 if KeywordArguments.fully_supported?135 subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)136 def #{method_name}(*args, **kwargs, &block)137 arguments = MethodArguments.new(args, kwargs, block)138 obj = ::RR::Injections::DoubleInjection::BoundObjects[#{id}]139 ::RR::Injections::DoubleInjection.dispatch_method(140 self,141 obj,142 :#{method_name},143 arguments.arguments,144 arguments.keyword_arguments,145 arguments.block146 )147 end148 RUBY149 else150 subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)151 def #{method_name}(*args, &block)152 arguments = MethodArguments.new(args, {}, block)153 obj = ::RR::Injections::DoubleInjection::BoundObjects[#{id}]154 ::RR::Injections::DoubleInjection.dispatch_method(155 self,156 obj,157 :#{method_name},158 arguments.arguments,159 arguments.keyword_arguments,160 arguments.block161 )162 end163 ruby2_keywords(:#{method_name}) if respond_to?(:ruby2_keywords, true)164 RUBY165 end166 self167 end168 # RR::DoubleInjection#verify verifies each Double169 # TimesCalledExpectation are met.170 def verify171 @doubles.each do |double|172 double.verify173 end174 end175 # RR::DoubleInjection#reset removes the injected dispatcher method.176 # It binds the original method implementation on the subject177 # if one exists.178 def reset179 if subject_has_original_method?180 subject_class.__send__(:remove_method, method_name)181 subject_class.__send__(:alias_method, method_name, original_method_alias_name)182 subject_class.__send__(:remove_method, original_method_alias_name)183 else184 if subject_has_method_defined?(method_name)185 subject_class.__send__(:remove_method, method_name)186 end187 end188 end189 def dispatch_method(subject, args, kwargs, block)190 if @dispatch_method_delegates_to_dispatch_original_method191 dispatch_original_method(subject, args, kwargs, block)192 else193 dispatch = MethodDispatches::MethodDispatch.new(194 self,195 subject,196 args,197 kwargs,198 block199 )200 dispatch.call201 end202 end203 def dispatch_original_method(subject, args, kwargs, block)204 dispatch = MethodDispatches::MethodDispatch.new(205 self,206 subject,207 args,208 kwargs,209 block210 )211 dispatch.call_original_method212 end213 def subject_has_original_method_missing?214 class_instance_method_defined(subject_class, MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name)215 end216 def original_method_alias_name217 "__rr__original_#{@method_name}"218 end219 def dispatch_method_delegates_to_dispatch_original_method220 @dispatch_method_delegates_to_dispatch_original_method = true221 yield222 ensure223 @dispatch_method_delegates_to_dispatch_original_method = nil224 end225 protected226 def deferred_bind_method227 if respond_to?(method_name) and228 not subject_has_method_defined?(original_method_alias_name)229 bind_method_with_alias230 end231 @performed_deferred_bind = true232 end233 def bind_method_with_alias234 subject_class.__send__(:alias_method, original_method_alias_name, method_name)235 bind_method236 end237 end238 end239end...
space.rb
Source:space.rb
...12 end13 attr_writer :instance14 protected15 if KeywordArguments.fully_supported?16 def method_missing(method_name, *args, **kwargs, &block)17 instance.__send__(method_name, *args, **kwargs, &block)18 end19 else20 def method_missing(method_name, *args, &block)21 instance.__send__(method_name, *args, &block)22 end23 ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)24 end25 end26 attr_reader :ordered_doubles, :recorded_calls27 attr_accessor :trim_backtrace28 def initialize29 @ordered_doubles = []30 @trim_backtrace = false31 @recorded_calls = RR::RecordedCalls.new32 end33 # Registers the ordered Double to be verified.34 def register_ordered_double(double)35 @ordered_doubles << double unless ordered_doubles.include?(double)36 end37 # Verifies that the passed in ordered Double is being called38 # in the correct position.39 def verify_ordered_double(double)40 unless double.terminal?41 raise RR::Errors.build_error(:DoubleOrderError,42 "Ordered Doubles cannot have a NonTerminal TimesCalledExpectation")43 end44 unless @ordered_doubles.first == double45 message = Double.formatted_name(double.method_name, double.expected_arguments)46 message << " called out of order in list\n"47 message << Double.list_message_part(@ordered_doubles)48 raise RR::Errors.build_error(:DoubleOrderError, message)49 end50 @ordered_doubles.shift unless double.attempt?51 double52 end53 # Verifies all the DoubleInjection objects have met their54 # TimesCalledExpectations.55 def verify_doubles(*subjects)56 Injections::DoubleInjection.verify(*subjects)57 end58 alias_method :verify, :verify_doubles59 # Resets the registered Doubles and ordered Doubles60 def reset61 RR.trim_backtrace = false62 RR.overridden_error_class = nil63 reset_ordered_doubles64 Injections::DoubleInjection.reset65 reset_method_missing_injections66 reset_singleton_method_added_injections67 reset_recorded_calls68 reset_bound_objects69 end70 # Verifies the DoubleInjection for the passed in subject and method_name.71 def verify_double(subject, method_name)72 Injections::DoubleInjection.verify_double(class << subject; self; end, method_name)73 end74 # Resets the DoubleInjection for the passed in subject and method_name.75 def reset_double(subject, method_name)76 Injections::DoubleInjection.reset_double(class << subject; self; end, method_name)77 end78 def record_call(subject, method_name, arguments, keyword_arguments, block)79 @recorded_calls.add(subject,80 method_name,81 arguments,82 keyword_arguments,83 block)84 end85 def blank_slate_whitelist86 @blank_slate_whitelist ||= [87 "object_id", "respond_to?", "respond_to_missing?", "method_missing", "instance_eval", "instance_exec", "class_eval"88 ]89 end90 protected91 # Removes the ordered Doubles from the list92 def reset_ordered_doubles93 @ordered_doubles.clear94 end...
rspec_adapter_spec.rb
Source:rspec_adapter_spec.rb
1require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")2module RR3 module Adapters4 describe Rspec do5 attr_reader :fixture, :subject, :method_name6 describe "#setup_mocks_for_rspec" do7 before do8 @fixture = Object.new9 fixture.extend Rspec10 @subject = Object.new11 @method_name = :foobar12 end13 it "resets the double_injections" do14 RR.double_injection(subject, method_name)15 RR.double_injections.should_not be_empty16 fixture.setup_mocks_for_rspec17 RR.double_injections.should be_empty18 end19 end20 describe "#verify_mocks_for_rspec" do21 before do22 @fixture = Object.new23 fixture.extend Rspec24 @subject = Object.new25 @method_name = :foobar26 end27 it "verifies the double_injections" do28 double_injection = RR.double_injection(subject, method_name)29 double = new_double(double_injection)30 double.definition.once31 lambda do32 fixture.verify_mocks_for_rspec33 end.should raise_error(::RR::Errors::TimesCalledError)34 RR.double_injections.should be_empty35 end36 end37 describe "#teardown_mocks_for_rspec" do38 before do39 @fixture = Object.new40 fixture.extend Rspec41 @subject = Object.new42 @method_name = :foobar43 end44 it "resets the double_injections" do45 RR.double_injection(subject, method_name)46 RR.double_injections.should_not be_empty47 fixture.teardown_mocks_for_rspec48 RR.double_injections.should be_empty49 end50 end51 end52 end53end...
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!!