How to use instance method of context Package

Best Vcr_ruby code snippet using context.instance

any_instance_spec.rb

Source:any_instance_spec.rb Github

copy

Full Screen

1require 'spec_helper'2require 'delegate'3module RSpec4 module Mocks5 describe "#any_instance" do6 class CustomErrorForAnyInstanceSpec < StandardError;end7 let(:klass) do8 Class.new do9 def existing_method; :existing_method_return_value; end10 def existing_method_with_arguments(arg_one, arg_two = nil); :existing_method_with_arguments_return_value; end11 def another_existing_method; end12 private13 def private_method; :private_method_return_value; end14 end15 end16 let(:existing_method_return_value){ :existing_method_return_value }17 context "invocation order" do18 context "#stub" do19 it "raises an error if 'stub' follows 'with'" do20 expect { klass.any_instance.with("1").stub(:foo) }.to raise_error(NoMethodError)21 end22 it "raises an error if 'with' follows 'and_return'" do23 expect { klass.any_instance.stub(:foo).and_return(1).with("1") }.to raise_error(NoMethodError)24 end25 it "raises an error if 'with' follows 'and_raise'" do26 expect { klass.any_instance.stub(:foo).and_raise(1).with("1") }.to raise_error(NoMethodError)27 end28 it "raises an error if 'with' follows 'and_yield'" do29 expect { klass.any_instance.stub(:foo).and_yield(1).with("1") }.to raise_error(NoMethodError)30 end31 end32 context "#stub_chain" do33 it "raises an error if 'stub_chain' follows 'any_instance'" do34 expect { klass.any_instance.and_return("1").stub_chain(:foo, :bar) }.to raise_error(NoMethodError)35 end36 end37 context "#should_receive" do38 it "raises an error if 'should_receive' follows 'with'" do39 expect { klass.any_instance.with("1").should_receive(:foo) }.to raise_error(NoMethodError)40 end41 it "raises an error if 'with' follows 'and_return'" do42 pending "see Github issue #42"43 expect { klass.any_instance.should_receive(:foo).and_return(1).with("1") }.to raise_error(NoMethodError)44 end45 it "raises an error if 'with' follows 'and_raise'" do46 pending "see Github issue #42"47 expect { klass.any_instance.should_receive(:foo).and_raise(1).with("1") }.to raise_error(NoMethodError)48 end49 end50 end51 context "with #stub" do52 it "does not suppress an exception when a method that doesn't exist is invoked" do53 klass.any_instance.stub(:foo)54 expect { klass.new.bar }.to raise_error(NoMethodError)55 end56 context 'multiple methods' do57 it "allows multiple methods to be stubbed in a single invocation" do58 klass.any_instance.stub(:foo => 'foo', :bar => 'bar')59 instance = klass.new60 expect(instance.foo).to eq('foo')61 expect(instance.bar).to eq('bar')62 end63 it "adheres to the contract of multiple method stubbing withou any instance" do64 expect(Object.new.stub(:foo => 'foo', :bar => 'bar')).to eq(:foo => 'foo', :bar => 'bar')65 expect(klass.any_instance.stub(:foo => 'foo', :bar => 'bar')).to eq(:foo => 'foo', :bar => 'bar')66 end67 context "allows a chain of methods to be stubbed using #stub_chain" do68 it "given symbols representing the methods" do69 klass.any_instance.stub_chain(:one, :two, :three).and_return(:four)70 expect(klass.new.one.two.three).to eq(:four)71 end72 it "given a hash as the last argument uses the value as the expected return value" do73 klass.any_instance.stub_chain(:one, :two, :three => :four)74 expect(klass.new.one.two.three).to eq(:four)75 end76 it "given a string of '.' separated method names representing the chain" do77 klass.any_instance.stub_chain('one.two.three').and_return(:four)78 expect(klass.new.one.two.three).to eq(:four)79 end80 end81 end82 context "behaves as 'every instance'" do83 it "stubs every instance in the spec" do84 klass.any_instance.stub(:foo).and_return(result = Object.new)85 expect(klass.new.foo).to eq(result)86 expect(klass.new.foo).to eq(result)87 end88 it "stubs instance created before any_instance was called" do89 instance = klass.new90 klass.any_instance.stub(:foo).and_return(result = Object.new)91 expect(instance.foo).to eq(result)92 end93 end94 context "with argument matching" do95 before do96 klass.any_instance.stub(:foo).with(:param_one, :param_two).and_return(:result_one)97 klass.any_instance.stub(:foo).with(:param_three, :param_four).and_return(:result_two)98 end99 it "returns the stubbed value when arguments match" do100 instance = klass.new101 expect(instance.foo(:param_one, :param_two)).to eq(:result_one)102 expect(instance.foo(:param_three, :param_four)).to eq(:result_two)103 end104 it "fails the spec with an expectation error when the arguments do not match" do105 expect do106 klass.new.foo(:param_one, :param_three)107 end.to(raise_error(RSpec::Mocks::MockExpectationError))108 end109 end110 context "with multiple stubs" do111 before do112 klass.any_instance.stub(:foo).and_return(1)113 klass.any_instance.stub(:bar).and_return(2)114 end115 it "stubs a method" do116 instance = klass.new117 expect(instance.foo).to eq(1)118 expect(instance.bar).to eq(2)119 end120 it "returns the same value for calls on different instances" do121 expect(klass.new.foo).to eq(klass.new.foo)122 expect(klass.new.bar).to eq(klass.new.bar)123 end124 end125 context "with #and_return" do126 it "stubs a method that doesn't exist" do127 klass.any_instance.stub(:foo).and_return(1)128 expect(klass.new.foo).to eq(1)129 end130 it "stubs a method that exists" do131 klass.any_instance.stub(:existing_method).and_return(1)132 expect(klass.new.existing_method).to eq(1)133 end134 it "returns the same object for calls on different instances" do135 return_value = Object.new136 klass.any_instance.stub(:foo).and_return(return_value)137 expect(klass.new.foo).to be(return_value)138 expect(klass.new.foo).to be(return_value)139 end140 end141 context "with #and_yield" do142 it "yields the value specified" do143 yielded_value = Object.new144 klass.any_instance.stub(:foo).and_yield(yielded_value)145 klass.new.foo{|value| expect(value).to be(yielded_value)}146 end147 end148 context "with #and_raise" do149 it "stubs a method that doesn't exist" do150 klass.any_instance.stub(:foo).and_raise(CustomErrorForAnyInstanceSpec)151 expect { klass.new.foo}.to raise_error(CustomErrorForAnyInstanceSpec)152 end153 it "stubs a method that exists" do154 klass.any_instance.stub(:existing_method).and_raise(CustomErrorForAnyInstanceSpec)155 expect { klass.new.existing_method}.to raise_error(CustomErrorForAnyInstanceSpec)156 end157 end158 context "with a block" do159 it "stubs a method" do160 klass.any_instance.stub(:foo) { 1 }161 expect(klass.new.foo).to eq(1)162 end163 it "returns the same computed value for calls on different instances" do164 klass.any_instance.stub(:foo) { 1 + 2 }165 expect(klass.new.foo).to eq(klass.new.foo)166 end167 end168 context "core ruby objects" do169 it "works uniformly across *everything*" do170 Object.any_instance.stub(:foo).and_return(1)171 expect(Object.new.foo).to eq(1)172 end173 it "works with the non-standard constructor []" do174 Array.any_instance.stub(:foo).and_return(1)175 expect([].foo).to eq(1)176 end177 it "works with the non-standard constructor {}" do178 Hash.any_instance.stub(:foo).and_return(1)179 expect({}.foo).to eq(1)180 end181 it "works with the non-standard constructor \"\"" do182 String.any_instance.stub(:foo).and_return(1)183 expect("".foo).to eq(1)184 end185 it "works with the non-standard constructor \'\'" do186 String.any_instance.stub(:foo).and_return(1)187 expect(''.foo).to eq(1)188 end189 it "works with the non-standard constructor module" do190 Module.any_instance.stub(:foo).and_return(1)191 module RSpec::SampleRspecTestModule;end192 expect(RSpec::SampleRspecTestModule.foo).to eq(1)193 end194 it "works with the non-standard constructor class" do195 Class.any_instance.stub(:foo).and_return(1)196 class RSpec::SampleRspecTestClass;end197 expect(RSpec::SampleRspecTestClass.foo).to eq(1)198 end199 end200 end201 context "with #stub!" do202 it "raises with a message instructing the user to use stub instead" do203 expect do204 klass.any_instance.stub!(:foo)205 end.to raise_error(/Use stub instead/)206 end207 end208 context "with #unstub!" do209 it "raises with a message instructing the user to use unstub instead" do210 expect do211 klass.any_instance.unstub!(:foo)212 end.to raise_error(/Use unstub instead/)213 end214 end215 context "unstub implementation" do216 it "replaces the stubbed method with the original method" do217 klass.any_instance.stub(:existing_method)218 klass.any_instance.unstub(:existing_method)219 expect(klass.new.existing_method).to eq(:existing_method_return_value)220 end221 it "removes all stubs with the supplied method name" do222 klass.any_instance.stub(:existing_method).with(1)223 klass.any_instance.stub(:existing_method).with(2)224 klass.any_instance.unstub(:existing_method)225 expect(klass.new.existing_method).to eq(:existing_method_return_value)226 end227 it "does not remove any expectations with the same method name" do228 klass.any_instance.should_receive(:existing_method_with_arguments).with(3).and_return(:three)229 klass.any_instance.stub(:existing_method_with_arguments).with(1)230 klass.any_instance.stub(:existing_method_with_arguments).with(2)231 klass.any_instance.unstub(:existing_method_with_arguments)232 expect(klass.new.existing_method_with_arguments(3)).to eq(:three)233 end234 it "raises a MockExpectationError if the method has not been stubbed" do235 expect {236 klass.any_instance.unstub(:existing_method)237 }.to raise_error(RSpec::Mocks::MockExpectationError, 'The method `existing_method` was not stubbed or was already unstubbed')238 end239 end240 context "with #should_not_receive" do241 it "fails if the method is called" do242 klass.any_instance.should_not_receive(:existing_method)243 expect { klass.new.existing_method }.to raise_error(RSpec::Mocks::MockExpectationError)244 end245 it "passes if no method is called" do246 expect { klass.any_instance.should_not_receive(:existing_method) }.to_not raise_error247 end248 it "passes if only a different method is called" do249 klass.any_instance.should_not_receive(:existing_method)250 expect { klass.new.another_existing_method }.to_not raise_error251 end252 context "with constraints" do253 it "fails if the method is called with the specified parameters" do254 klass.any_instance.should_not_receive(:existing_method_with_arguments).with(:argument_one, :argument_two)255 expect {256 klass.new.existing_method_with_arguments(:argument_one, :argument_two)257 }.to raise_error(RSpec::Mocks::MockExpectationError)258 end259 it "passes if the method is called with different parameters" do260 klass.any_instance.should_not_receive(:existing_method_with_arguments).with(:argument_one, :argument_two)261 expect { klass.new.existing_method_with_arguments(:argument_three, :argument_four) }.to_not raise_error262 end263 end264 context 'when used in combination with should_receive' do265 it 'passes if only the expected message is received' do266 klass.any_instance.should_receive(:foo)267 klass.any_instance.should_not_receive(:bar)268 klass.new.foo269 klass.rspec_verify270 end271 end272 end273 context "with #should_receive" do274 let(:foo_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: foo' }275 let(:existing_method_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: existing_method' }276 context "with an expectation is set on a method which does not exist" do277 it "returns the expected value" do278 klass.any_instance.should_receive(:foo).and_return(1)279 expect(klass.new.foo(1)).to eq(1)280 end281 it "fails if an instance is created but no invocation occurs" do282 expect do283 klass.any_instance.should_receive(:foo)284 klass.new285 klass.rspec_verify286 end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)287 end288 it "fails if no instance is created" do289 expect do290 klass.any_instance.should_receive(:foo).and_return(1)291 klass.rspec_verify292 end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)293 end294 it "fails if no instance is created and there are multiple expectations" do295 expect do296 klass.any_instance.should_receive(:foo)297 klass.any_instance.should_receive(:bar)298 klass.rspec_verify299 end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: bar, foo')300 end301 it "allows expectations on instances to take priority" do302 klass.any_instance.should_receive(:foo)303 klass.new.foo304 instance = klass.new305 instance.should_receive(:foo).and_return(result = Object.new)306 expect(instance.foo).to eq(result)307 end308 context "behaves as 'exactly one instance'" do309 it "passes if subsequent invocations do not receive that message" do310 klass.any_instance.should_receive(:foo)311 klass.new.foo312 klass.new313 end314 it "fails if the method is invoked on a second instance" do315 instance_one = klass.new316 instance_two = klass.new317 expect do318 klass.any_instance.should_receive(:foo)319 instance_one.foo320 instance_two.foo321 end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'foo' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")322 end323 end324 context "normal expectations on the class object" do325 it "fail when unfulfilled" do326 expect do327 klass.any_instance.should_receive(:foo)328 klass.should_receive(:woot)329 klass.new.foo330 klass.rspec_verify331 end.to(raise_error(RSpec::Mocks::MockExpectationError) do |error|332 expect(error.message).not_to eq(existing_method_expectation_error_message)333 end)334 end335 it "pass when expectations are met" do336 klass.any_instance.should_receive(:foo)337 klass.should_receive(:woot).and_return(result = Object.new)338 klass.new.foo339 expect(klass.woot).to eq(result)340 end341 end342 end343 context "with an expectation is set on a method that exists" do344 it "returns the expected value" do345 klass.any_instance.should_receive(:existing_method).and_return(1)346 expect(klass.new.existing_method(1)).to eq(1)347 end348 it "fails if an instance is created but no invocation occurs" do349 expect do350 klass.any_instance.should_receive(:existing_method)351 klass.new352 klass.rspec_verify353 end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)354 end355 it "fails if no instance is created" do356 expect do357 klass.any_instance.should_receive(:existing_method)358 klass.rspec_verify359 end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)360 end361 it "fails if no instance is created and there are multiple expectations" do362 expect do363 klass.any_instance.should_receive(:existing_method)364 klass.any_instance.should_receive(:another_existing_method)365 klass.rspec_verify366 end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: another_existing_method, existing_method')367 end368 context "after any one instance has received a message" do369 it "passes if subsequent invocations do not receive that message" do370 klass.any_instance.should_receive(:existing_method)371 klass.new.existing_method372 klass.new373 end374 it "fails if the method is invoked on a second instance" do375 instance_one = klass.new376 instance_two = klass.new377 expect do378 klass.any_instance.should_receive(:existing_method)379 instance_one.existing_method380 instance_two.existing_method381 end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'existing_method' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")382 end383 end384 end385 it 'works with a BasicObject subclass that mixes in Kernel', :if => defined?(BasicObject) do386 klass = Class.new(BasicObject) do387 include ::Kernel388 def foo; end389 end390 klass.any_instance.should_receive(:foo)391 klass.new.foo392 end393 it 'works with a SimpleDelegator subclass', :if => (RUBY_VERSION.to_f > 1.8) do394 klass = Class.new(SimpleDelegator) do395 def foo; end396 end397 klass.any_instance.should_receive(:foo)398 klass.new(Object.new).foo399 end400 context "with argument matching" do401 before do402 klass.any_instance.should_receive(:foo).with(:param_one, :param_two).and_return(:result_one)403 klass.any_instance.should_receive(:foo).with(:param_three, :param_four).and_return(:result_two)404 end405 it "returns the expected value when arguments match" do406 instance = klass.new407 expect(instance.foo(:param_one, :param_two)).to eq(:result_one)408 expect(instance.foo(:param_three, :param_four)).to eq(:result_two)409 end410 it "fails when the arguments match but different instances are used" do411 instances = Array.new(2) { klass.new }412 expect do413 expect(instances[0].foo(:param_one, :param_two)).to eq(:result_one)414 expect(instances[1].foo(:param_three, :param_four)).to eq(:result_two)415 end.to raise_error(RSpec::Mocks::MockExpectationError)416 # ignore the fact that should_receive expectations were not met417 instances.each { |instance| instance.rspec_reset }418 end419 it "is not affected by the invocation of existing methods on other instances" do420 expect(klass.new.existing_method_with_arguments(:param_one, :param_two)).to eq(:existing_method_with_arguments_return_value)421 instance = klass.new422 expect(instance.foo(:param_one, :param_two)).to eq(:result_one)423 expect(instance.foo(:param_three, :param_four)).to eq(:result_two)424 end425 it "fails when arguments do not match" do426 instance = klass.new427 expect do428 instance.foo(:param_one, :param_three)429 end.to raise_error(RSpec::Mocks::MockExpectationError)430 # ignore the fact that should_receive expectations were not met431 instance.rspec_reset432 end433 end434 context "message count" do435 context "the 'once' constraint" do436 it "passes for one invocation" do437 klass.any_instance.should_receive(:foo).once438 klass.new.foo439 end440 it "fails when no instances are declared" do441 expect do442 klass.any_instance.should_receive(:foo).once443 klass.rspec_verify444 end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)445 end446 it "fails when an instance is declared but there are no invocations" do447 expect do448 klass.any_instance.should_receive(:foo).once449 klass.new450 klass.rspec_verify451 end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)452 end453 it "fails for more than one invocation" do454 expect do455 klass.any_instance.should_receive(:foo).once456 instance = klass.new457 2.times { instance.foo }458 instance.rspec_verify459 end.to raise_error(RSpec::Mocks::MockExpectationError)460 end461 end462 context "the 'twice' constraint" do463 it "passes for two invocations" do464 klass.any_instance.should_receive(:foo).twice465 instance = klass.new466 2.times { instance.foo }467 end468 it "fails for more than two invocations" do469 expect do470 klass.any_instance.should_receive(:foo).twice471 instance = klass.new472 3.times { instance.foo }473 instance.rspec_verify474 end.to raise_error(RSpec::Mocks::MockExpectationError)475 end476 end477 context "the 'exactly(n)' constraint" do478 it "passes for n invocations where n = 3" do479 klass.any_instance.should_receive(:foo).exactly(3).times480 instance = klass.new481 3.times { instance.foo }482 end483 it "fails for n invocations where n < 3" do484 expect do485 klass.any_instance.should_receive(:foo).exactly(3).times486 instance = klass.new487 2.times { instance.foo }488 instance.rspec_verify489 end.to raise_error(RSpec::Mocks::MockExpectationError)490 end491 it "fails for n invocations where n > 3" do492 expect do493 klass.any_instance.should_receive(:foo).exactly(3).times494 instance = klass.new495 4.times { instance.foo }496 instance.rspec_verify497 end.to raise_error(RSpec::Mocks::MockExpectationError)498 end499 end500 context "the 'at_least(n)' constraint" do501 it "passes for n invocations where n = 3" do502 klass.any_instance.should_receive(:foo).at_least(3).times503 instance = klass.new504 3.times { instance.foo }505 end506 it "fails for n invocations where n < 3" do507 expect do508 klass.any_instance.should_receive(:foo).at_least(3).times509 instance = klass.new510 2.times { instance.foo }511 instance.rspec_verify512 end.to raise_error(RSpec::Mocks::MockExpectationError)513 end514 it "passes for n invocations where n > 3" do515 klass.any_instance.should_receive(:foo).at_least(3).times516 instance = klass.new517 4.times { instance.foo }518 end519 end520 context "the 'at_most(n)' constraint" do521 it "passes for n invocations where n = 3" do522 klass.any_instance.should_receive(:foo).at_most(3).times523 instance = klass.new524 3.times { instance.foo }525 end526 it "passes for n invocations where n < 3" do527 klass.any_instance.should_receive(:foo).at_most(3).times528 instance = klass.new529 2.times { instance.foo }530 end531 it "fails for n invocations where n > 3" do532 expect do533 klass.any_instance.should_receive(:foo).at_most(3).times534 instance = klass.new535 4.times { instance.foo }536 instance.rspec_verify537 end.to raise_error(RSpec::Mocks::MockExpectationError)538 end539 end540 context "the 'never' constraint" do541 it "passes for 0 invocations" do542 klass.any_instance.should_receive(:foo).never543 klass.rspec_verify544 end545 it "fails on the first invocation" do546 expect do547 klass.any_instance.should_receive(:foo).never548 klass.new.foo549 end.to raise_error(RSpec::Mocks::MockExpectationError)550 end551 context "when combined with other expectations" do552 it "passes when the other expecations are met" do553 klass.any_instance.should_receive(:foo).never554 klass.any_instance.should_receive(:existing_method).and_return(5)555 expect(klass.new.existing_method).to eq(5)556 end557 it "fails when the other expecations are not met" do558 expect do559 klass.any_instance.should_receive(:foo).never560 klass.any_instance.should_receive(:existing_method).and_return(5)561 klass.rspec_verify562 end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)563 end564 end565 end566 context "the 'any_number_of_times' constraint" do567 it "passes for 0 invocations" do568 klass.any_instance.should_receive(:foo).any_number_of_times569 klass.new.rspec_verify570 end571 it "passes for a non-zero number of invocations" do572 klass.any_instance.should_receive(:foo).any_number_of_times573 instance = klass.new574 instance.foo575 instance.rspec_verify576 end577 it "does not interfere with other expectations" do578 klass.any_instance.should_receive(:foo).any_number_of_times579 klass.any_instance.should_receive(:existing_method).and_return(5)580 expect(klass.new.existing_method).to eq(5)581 end582 context "when combined with other expectations" do583 it "passes when the other expecations are met" do584 klass.any_instance.should_receive(:foo).any_number_of_times585 klass.any_instance.should_receive(:existing_method).and_return(5)586 expect(klass.new.existing_method).to eq(5)587 end588 it "fails when the other expecations are not met" do589 expect do590 klass.any_instance.should_receive(:foo).any_number_of_times591 klass.any_instance.should_receive(:existing_method).and_return(5)592 klass.rspec_verify593 end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)594 end595 end596 end597 end598 end599 context "when resetting post-verification" do600 let(:space) { RSpec::Mocks::Space.new }601 context "existing method" do602 before(:each) do603 space.add(klass)604 end605 context "with stubbing" do606 context "public methods" do607 before(:each) do608 klass.any_instance.stub(:existing_method).and_return(1)609 expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_true610 end611 it "restores the class to its original state after each example when no instance is created" do612 space.verify_all613 expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false614 expect(klass.new.existing_method).to eq(existing_method_return_value)615 end616 it "restores the class to its original state after each example when one instance is created" do617 klass.new.existing_method618 space.verify_all619 expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false620 expect(klass.new.existing_method).to eq(existing_method_return_value)621 end622 it "restores the class to its original state after each example when more than one instance is created" do623 klass.new.existing_method624 klass.new.existing_method625 space.verify_all626 expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false627 expect(klass.new.existing_method).to eq(existing_method_return_value)628 end629 end630 context "private methods" do631 before :each do632 klass.any_instance.stub(:private_method).and_return(:something)633 space.verify_all634 end635 it "cleans up the backed up method" do636 expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false637 end638 it "restores a stubbed private method after the spec is run" do639 expect(klass.private_method_defined?(:private_method)).to be_true640 end641 it "ensures that the restored method behaves as it originally did" do642 expect(klass.new.send(:private_method)).to eq(:private_method_return_value)643 end644 end645 end646 context "with expectations" do647 context "private methods" do648 before :each do649 klass.any_instance.should_receive(:private_method).and_return(:something)650 klass.new.private_method651 space.verify_all652 end653 it "cleans up the backed up method" do654 expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false655 end656 it "restores a stubbed private method after the spec is run" do657 expect(klass.private_method_defined?(:private_method)).to be_true658 end659 it "ensures that the restored method behaves as it originally did" do660 expect(klass.new.send(:private_method)).to eq(:private_method_return_value)661 end662 end663 context "ensures that the subsequent specs do not see expectations set in previous specs" do664 context "when the instance created after the expectation is set" do665 it "first spec" do666 klass.any_instance.should_receive(:existing_method).and_return(Object.new)667 klass.new.existing_method668 end669 it "second spec" do670 expect(klass.new.existing_method).to eq(existing_method_return_value)671 end672 end673 context "when the instance created before the expectation is set" do674 before :each do675 @instance = klass.new676 end677 it "first spec" do678 klass.any_instance.should_receive(:existing_method).and_return(Object.new)679 @instance.existing_method680 end681 it "second spec" do682 expect(@instance.existing_method).to eq(existing_method_return_value)683 end684 end685 end686 it "ensures that the next spec does not see that expectation" do687 klass.any_instance.should_receive(:existing_method).and_return(Object.new)688 klass.new.existing_method689 space.verify_all690 expect(klass.new.existing_method).to eq(existing_method_return_value)691 end692 end693 end694 context "with multiple calls to any_instance in the same example" do695 it "does not prevent the change from being rolled back" do696 klass.any_instance.stub(:existing_method).and_return(false)697 klass.any_instance.stub(:existing_method).and_return(true)698 klass.rspec_verify699 expect(klass.new).to respond_to(:existing_method)700 expect(klass.new.existing_method).to eq(existing_method_return_value)701 end702 end703 it "adds an class to the current space when #any_instance is invoked" do704 klass.any_instance705 expect(RSpec::Mocks::space.send(:receivers)).to include(klass)706 end707 it "adds an instance to the current space when stubbed method is invoked" do708 klass.any_instance.stub(:foo)709 instance = klass.new710 instance.foo711 expect(RSpec::Mocks::space.send(:receivers)).to include(instance)712 end713 end714 context 'when used in conjunction with a `dup`' do715 it "doesn't cause an infinite loop" do716 Object.any_instance.stub(:some_method)717 o = Object.new718 o.some_method719 expect { o.dup.some_method }.to_not raise_error(SystemStackError)720 end721 it "doesn't bomb if the object doesn't support `dup`" do722 klass = Class.new do723 undef_method :dup724 end725 klass.any_instance726 end727 it "doesn't fail when dup accepts parameters" do728 klass = Class.new do729 def dup(funky_option)730 end731 end732 klass.any_instance733 expect { klass.new.dup('Dup dup dup') }.to_not raise_error(ArgumentError)734 end735 end736 context "when directed at a method defined on a superclass" do737 let(:sub_klass) { Class.new(klass) }738 it "stubs the method correctly" do739 klass.any_instance.stub(:existing_method).and_return("foo")740 expect(sub_klass.new.existing_method).to eq "foo"741 end742 it "mocks the method correctly" do743 instance_one = sub_klass.new744 instance_two = sub_klass.new745 expect do746 klass.any_instance.should_receive(:existing_method)747 instance_one.existing_method748 instance_two.existing_method749 end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'existing_method' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")750 end751 end752 context "when a class overrides Object#method" do753 let(:http_request_class) { Struct.new(:method, :uri) }754 it "stubs the method correctly" do755 http_request_class.any_instance.stub(:existing_method).and_return("foo")756 expect(http_request_class.new.existing_method).to eq "foo"757 end758 it "mocks the method correctly" do759 http_request_class.any_instance.should_receive(:existing_method).and_return("foo")760 expect(http_request_class.new.existing_method).to eq "foo"761 end762 end763 context "when used after the test has finished" do764 it "restores the original behavior of a stubbed method" do765 klass.any_instance.stub(:existing_method).and_return(:stubbed_return_value)766 instance = klass.new767 expect(instance.existing_method).to eq :stubbed_return_value768 RSpec::Mocks.verify769 expect(instance.existing_method).to eq :existing_method_return_value770 end771 end772 end773 end774end...

Full Screen

Full Screen

validations_spec.rb

Source:validations_spec.rb Github

copy

Full Screen

...11 let(:model_name) { "post" }12 let(:method) { double }13 let(:options) { Hash.new }14 let(:validator) { double }15 let(:instance) { MyInput.new(builder, template, model, model_name, method, options) }16 describe '#required?' do17 context 'with a single validator' do18 before :each do19 allow(instance).to receive(:validations?).and_return(:true)20 allow(instance).to receive(:validations).and_return([validator])21 end22 context 'with options[:required] being true' do23 let(:options) { {required: true} }24 it 'is required' do25 expect(instance.required?).to be_truthy26 end27 end28 context 'with options[:required] being false' do29 let(:options) { {required: false} }30 it 'is not required' do31 expect(instance.required?).to be_falsey32 end33 end34 context 'with negated validation' do35 it 'is not required' do36 instance.not_required_through_negated_validation!37 expect(instance.required?).to be_falsey38 end39 end40 context 'with presence validator' do41 let (:validator) { double(options: {}, kind: :presence) }42 it 'is required' do43 expect(instance.required?).to be_truthy44 end45 context 'with options[:on] as symbol' do46 context 'with save context' do47 let (:validator) { double(options: {on: :save}, kind: :presence) }48 it 'is required' do49 expect(instance.required?).to be_truthy50 end51 end52 context 'with create context' do53 let (:validator) { double(options: {on: :create}, kind: :presence) }54 it 'is required for new records' do55 allow(model).to receive(:new_record?).and_return(true)56 expect(instance.required?).to be_truthy57 end58 it 'is not required for existing records' do59 allow(model).to receive(:new_record?).and_return(false)60 expect(instance.required?).to be_falsey61 end62 end63 context 'with update context' do64 let (:validator) { double(options: {on: :update}, kind: :presence) }65 it 'is not required for new records' do66 allow(model).to receive(:new_record?).and_return(true)67 expect(instance.required?).to be_falsey68 end69 it 'is required for existing records' do70 allow(model).to receive(:new_record?).and_return(false)71 expect(instance.required?).to be_truthy72 end73 end74 end75 context 'with options[:on] as array' do76 context 'with save context' do77 let (:validator) { double(options: {on: [:save]}, kind: :presence) }78 it 'is required' do79 expect(instance.required?).to be_truthy80 end81 end82 context 'with create context' do83 let (:validator) { double(options: {on: [:create]}, kind: :presence) }84 it 'is required for new records' do85 allow(model).to receive(:new_record?).and_return(true)86 expect(instance.required?).to be_truthy87 end88 it 'is not required for existing records' do89 allow(model).to receive(:new_record?).and_return(false)90 expect(instance.required?).to be_falsey91 end92 end93 context 'with update context' do94 let (:validator) { double(options: {on: [:update]}, kind: :presence) }95 it 'is not required for new records' do96 allow(model).to receive(:new_record?).and_return(true)97 expect(instance.required?).to be_falsey98 end99 it 'is required for existing records' do100 allow(model).to receive(:new_record?).and_return(false)101 expect(instance.required?).to be_truthy102 end103 end104 context 'with save and create context' do105 let (:validator) { double(options: {on: [:save, :create]}, kind: :presence) }106 it 'is required for new records' do107 allow(model).to receive(:new_record?).and_return(true)108 expect(instance.required?).to be_truthy109 end110 it 'is required for existing records' do111 allow(model).to receive(:new_record?).and_return(false)112 expect(instance.required?).to be_truthy113 end114 end115 context 'with save and update context' do116 let (:validator) { double(options: {on: [:save, :create]}, kind: :presence) }117 it 'is required for new records' do118 allow(model).to receive(:new_record?).and_return(true)119 expect(instance.required?).to be_truthy120 end121 it 'is required for existing records' do122 allow(model).to receive(:new_record?).and_return(false)123 expect(instance.required?).to be_truthy124 end125 end126 context 'with create and update context' do127 let (:validator) { double(options: {on: [:create, :update]}, kind: :presence) }128 it 'is required for new records' do129 allow(model).to receive(:new_record?).and_return(true)130 expect(instance.required?).to be_truthy131 end132 it 'is required for existing records' do133 allow(model).to receive(:new_record?).and_return(false)134 expect(instance.required?).to be_truthy135 end136 end137 context 'with save and other context' do138 let (:validator) { double(options: {on: [:save, :foo]}, kind: :presence) }139 it 'is required for new records' do140 allow(model).to receive(:new_record?).and_return(true)141 expect(instance.required?).to be_truthy142 end143 it 'is required for existing records' do144 allow(model).to receive(:new_record?).and_return(false)145 expect(instance.required?).to be_truthy146 end147 end148 context 'with create and other context' do149 let (:validator) { double(options: {on: [:create, :foo]}, kind: :presence) }150 it 'is required for new records' do151 allow(model).to receive(:new_record?).and_return(true)152 expect(instance.required?).to be_truthy153 end154 it 'is not required for existing records' do155 allow(model).to receive(:new_record?).and_return(false)156 expect(instance.required?).to be_falsey157 end158 end159 context 'with update and other context' do160 let (:validator) { double(options: {on: [:update, :foo]}, kind: :presence) }161 it 'is not required for new records' do162 allow(model).to receive(:new_record?).and_return(true)163 expect(instance.required?).to be_falsey164 end165 it 'is required for existing records' do166 allow(model).to receive(:new_record?).and_return(false)167 expect(instance.required?).to be_truthy168 end169 end170 end171 end172 context 'with inclusion validator' do173 context 'with allow blank' do174 let (:validator) { double(options: {allow_blank: true}, kind: :inclusion) }175 it 'is not required' do176 expect(instance.required?).to be_falsey177 end178 end179 context 'without allow blank' do180 let (:validator) { double(options: {allow_blank: false}, kind: :inclusion) }181 it 'is required' do182 expect(instance.required?).to be_truthy183 end184 end185 end186 context 'with a length validator' do187 context 'with allow blank' do188 let (:validator) { double(options: {allow_blank: true}, kind: :length) }189 it 'is not required' do190 expect(instance.required?).to be_falsey191 end192 end193 context 'without allow blank' do194 let (:validator) { double(options: {allow_blank: false}, kind: :length) }195 it 'is not required' do196 expect(instance.required?).to be_falsey197 end198 context 'with a minimum > 0' do199 let (:validator) { double(options: {allow_blank: false, minimum: 1}, kind: :length) }200 it 'is required' do201 expect(instance.required?).to be_truthy202 end203 end204 context 'with a minimum <= 0' do205 let (:validator) { double(options: {allow_blank: false, minimum: 0}, kind: :length) }206 it 'is not required' do207 expect(instance.required?).to be_falsey208 end209 end210 context 'with a defined range starting with > 0' do211 let (:validator) { double(options: {allow_blank: false, within: 1..5}, kind: :length) }212 it 'is required' do213 expect(instance.required?).to be_truthy214 end215 end216 context 'with a defined range starting with <= 0' do217 let (:validator) { double(options: {allow_blank: false, within: 0..5}, kind: :length) }218 it 'is not required' do219 expect(instance.required?).to be_falsey220 end221 end222 end223 end224 context 'with another validator' do225 let (:validator) { double(options: {allow_blank: true}, kind: :foo) }226 it 'is not required' do227 expect(instance.required?).to be_falsey228 end229 end230 end231 context 'with multiple validators' do232 context 'with a on create presence validator and a on update presence validator' do233 let (:validator1) { double(options: {on: :create}, kind: :presence) }234 let (:validator2) { double(options: {}, kind: :presence) }235 before :each do236 allow(model).to receive(:new_record?).and_return(false)237 allow(instance).to receive(:validations?).and_return(:true)238 allow(instance).to receive(:validations).and_return([validator1, validator2])239 end240 it 'is required' do241 expect(instance.required?).to be_truthy242 end243 end244 context 'with a on create presence validator and a presence validator' do245 let (:validator1) { double(options: {on: :create}, kind: :presence) }246 let (:validator2) { double(options: {}, kind: :presence) }247 before :each do248 allow(model).to receive(:new_record?).and_return(false)249 allow(instance).to receive(:validations?).and_return(:true)250 allow(instance).to receive(:validations).and_return([validator1, validator2])251 end252 it 'is required' do253 expect(instance.required?).to be_truthy254 end255 end256 context 'with a on create presence validator and a allow blank inclusion validator' do257 let (:validator1) { double(options: {on: :create}, kind: :presence) }258 let (:validator2) { double(options: {allow_blank: true}, kind: :inclusion) }259 before :each do260 allow(model).to receive(:new_record?).and_return(false)261 allow(instance).to receive(:validations?).and_return(:true)262 allow(instance).to receive(:validations).and_return([validator1, validator2])263 end264 it 'is required' do265 expect(instance.required?).to be_falsey266 end267 end268 end269 end270end...

Full Screen

Full Screen

context_spec.rb

Source:context_spec.rb Github

copy

Full Screen

...22 end23 describe "with context" do24 it "should allow eval within context" do25 File.expects(:read).with('my_file1').returns('method(1)')26 Blueprints::Context.any_instance.expects(:method).with(1)27 Blueprints::Context.eval_within_context(:file => 'my_file1')28 end29 it "should return context" do30 Blueprints::Context.eval_within_context(:dependencies => [:within_dep]).dependencies.should == [:within_dep]31 end32 it "should instance eval with current context set" do33 Blueprints::Context.any_instance.expects(:custom_method).with(1)34 Blueprints::Context.send(:class_variable_get, :@@chain) << context35 Blueprints::Context.eval_within_context(:dependencies => [:within_dep]) do36 custom_method(1)37 Blueprints::Context.current.dependencies.should == [:within_dep]38 end39 Blueprints::Context.current.should == context40 Blueprints::Context.send(:class_variable_get, :@@chain).pop41 end42 end43 describe "child contexts" do44 it "should yield and return child context with attributes" do45 yielded_context = nil46 subject.attributes(:attr3 => 3) { yielded_context = self }.should == yielded_context47 yielded_context.should be_instance_of(Blueprints::Context)48 yielded_context.attributes.should == {:attr1 => 1, :attr2 => 2, :attr3 => 3}49 end50 it "should yield and return child context with dependencies" do51 yielded_context = nil52 subject.depends_on('the_dep') { yielded_context = self }.should == yielded_context53 yielded_context.should be_instance_of(Blueprints::Context)54 yielded_context.dependencies.should == [:dep1, :dep2, :the_dep]55 end56 end57 describe "blueprints and namespaces" do58 it "should allow creating blueprint" do59 blueprint = subject.blueprint :blueprint60 blueprint.should be_instance_of(Blueprints::Blueprint)61 blueprint.name.should == :blueprint62 blueprint.instance_variable_get(:@context).should == subject63 end64 it "should allow creating namespace" do65 namespace = subject.namespace :namespace66 namespace.should be_instance_of(Blueprints::Namespace)67 namespace.name.should == :namespace68 namespace.instance_variable_get(:@context).should == subject69 end70 it "should allow creating blueprint inside namespace" do71 bp = nil72 namespace = subject.namespace(:namespace) { bp = self.blueprint :blueprint }73 bp.namespace.should == namespace74 namespace.children.should == [bp]75 end76 it "should allow creating blueprint with inferred name" do77 blueprint = subject.attributes(:name => 'bp').blueprint78 blueprint.name.should == :bp79 end80 it "should allow setting dependency" do81 dep = context.d(:bp, :option => 'val')82 dep.should be_instance_of(Blueprints::Dependency)83 end84 it "should allow finding blueprint you define" do85 blueprint = subject.blueprint :blueprint86 subject.find(:blueprint).should == blueprint87 subject[:blueprint].should == blueprint88 end89 end90end...

Full Screen

Full Screen

instance

Using AI Code Generation

copy

Full Screen

1def self.included(base)2def self.included(base)3def self.included(base)4def self.included(base)5def self.included(base)6def self.included(base)7def self.included(base)8def self.included(base)

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.

Run Vcr_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful