How to use foo method of RSpec Package

Best Test-prof_ruby code snippet using


Source:any_instance_spec.rb Github


Full Screen

...12 let(:existing_method_return_value){ :existing_method_return_value }13 context "invocation order" do14 context "#stub" do15 it "raises an error if 'stub' follows 'with'" do16 lambda{ klass.any_instance.with("1").stub(:foo) }.should raise_error(NoMethodError)17 end18 it "raises an error if 'with' follows 'and_return'" do19 lambda{ klass.any_instance.stub(:foo).and_return(1).with("1") }.should raise_error(NoMethodError)20 end21 it "raises an error if 'with' follows 'and_raise'" do22 lambda{ klass.any_instance.stub(:foo).and_raise(1).with("1") }.should raise_error(NoMethodError)23 end24 it "raises an error if 'with' follows 'and_yield'" do25 lambda{ klass.any_instance.stub(:foo).and_yield(1).with("1") }.should raise_error(NoMethodError)26 end27 end28 context "#should_receive" do29 it "raises an error if 'should_receive' follows 'with'" do30 lambda{ klass.any_instance.with("1").should_receive(:foo) }.should raise_error(NoMethodError)31 end32 it "raises an error if 'with' follows 'and_return'" do33 pending "see Github issue #42"34 lambda{ klass.any_instance.should_receive(:foo).and_return(1).with("1") }.should raise_error(NoMethodError)35 end36 it "raises an error if 'with' follows 'and_raise'" do37 pending "see Github issue #42"38 lambda{ klass.any_instance.should_receive(:foo).and_raise(1).with("1") }.should raise_error(NoMethodError)39 end40 end41 end42 context "with #stub" do43 it "does not suppress an exception when a method that doesn't exist is invoked" do44 klass.any_instance.stub(:foo)45 lambda{ }.should raise_error(NoMethodError)46 end47 context "behaves as 'every instance'" do48 it "stubs every instance in the spec" do49 klass.any_instance.stub(:foo).and_return(result = eq(result)51 eq(result)52 end53 it "stubs instance created before any_instance was called" do54 instance = klass.new55 klass.any_instance.stub(:foo).and_return(result = eq(result)57 end58 end59 context "with #and_return" do60 it "stubs a method that doesn't exist" do61 klass.any_instance.stub(:foo).and_return(1)62 eq(1)63 end64 it "stubs a method that exists" do65 klass.any_instance.stub(:existing_method).and_return(1)66 eq(1)67 end68 it "returns the same object for calls on different instances" do69 return_value = Object.new70 klass.any_instance.stub(:foo).and_return(return_value)71 be(return_value)72 be(return_value)73 end74 end75 context "with #and_yield" do76 it "yields the value specified" do77 yielded_value = Object.new78 klass.any_instance.stub(:foo).and_yield(yielded_value)79{|value| value.should be(yielded_value)}80 end81 end82 context "with #and_raise" do83 it "stubs a method that doesn't exist" do84 klass.any_instance.stub(:foo).and_raise(CustomErrorForAnyInstanceSpec)85 lambda{}.should raise_error(CustomErrorForAnyInstanceSpec)86 end87 it "stubs a method that exists" do88 klass.any_instance.stub(:existing_method).and_raise(CustomErrorForAnyInstanceSpec)89 lambda{}.should raise_error(CustomErrorForAnyInstanceSpec)90 end91 end92 context "with a block" do93 it "stubs a method" do94 klass.any_instance.stub(:foo) { 1 }95 eq(1)96 end97 it "returns the same computed value for calls on different instances" do98 klass.any_instance.stub(:foo) { 1 + 2 }99 eq( end101 end102 context "core ruby objects" do103 it "works uniformly across *everything*" do104 Object.any_instance.stub(:foo).and_return(1)105 eq(1)106 end107 it "works with the non-standard constructor []" do108 Array.any_instance.stub(:foo).and_return(1)109 [].foo.should eq(1)110 end111 it "works with the non-standard constructor {}" do112 Hash.any_instance.stub(:foo).and_return(1)113 {}.foo.should eq(1)114 end115 it "works with the non-standard constructor \"\"" do116 String.any_instance.stub(:foo).and_return(1)117 "".foo.should eq(1)118 end119 it "works with the non-standard constructor \'\'" do120 String.any_instance.stub(:foo).and_return(1)121 ''.foo.should eq(1)122 end123 it "works with the non-standard constructor module" do124 Module.any_instance.stub(:foo).and_return(1)125 module RSpec::SampleRspecTestModule;end126 eq(1)127 end128 it "works with the non-standard constructor class" do129 Class.any_instance.stub(:foo).and_return(1)130 class RSpec::SampleRspecTestClass;end131 eq(1)132 end133 end134 end135 context "with #should_receive" do136 let(:foo_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: foo' }137 let(:existing_method_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: existing_method' }138 context "with an expectation is set on a method which does not exist" do139 it "returns the expected value" do140 klass.any_instance.should_receive(:foo).and_return(1)141 eq(1)142 end143 it "fails if an instance is created but no invocation occurs" do144 expect do145 klass.any_instance.should_receive(:foo)146 klass.new147 klass.rspec_verify148 raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)149 end150 it "fails if no instance is created" do151 expect do152 klass.any_instance.should_receive(:foo).and_return(1)153 klass.rspec_verify154 raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)155 end156 it "fails if no instance is created and there are multiple expectations" do157 expect do158 klass.any_instance.should_receive(:foo)159 klass.any_instance.should_receive(:bar)160 klass.rspec_verify161 raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: bar, foo')162 end163 it "allows expectations on instances to take priority" do164 klass.any_instance.should_receive(:foo)165 instance = klass.new167 instance.should_receive(:foo).and_return(result = eq(result)169 end170 context "behaves as 'exactly one instance'" do171 it "passes if subsequent invocations do not receive that message" do172 klass.any_instance.should_receive(:foo)173 klass.new175 end176 it "fails if the method is invoked on a second instance" do177 instance_one = klass.new178 instance_two = klass.new179 expect do180 klass.any_instance.should_receive(:foo)181 instance_one.foo182 instance_two.foo183 raise_error(RSpec::Mocks::MockExpectationError, "The message 'foo' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")184 end185 end186 context "normal expectations on the class object" do187 it "fail when unfulfilled" do188 expect do189 klass.any_instance.should_receive(:foo)190 klass.should_receive(:woot)191 klass.rspec_verify193 do |error|194 error.message.should_not eq(existing_method_expectation_error_message)195 end)196 end197 it "pass when expectations are met" do198 klass.any_instance.should_receive(:foo)199 klass.should_receive(:woot).and_return(result = klass.woot.should eq(result)202 end203 end204 end205 context "with an expectation is set on a method that exists" do206 it "returns the expected value" do207 klass.any_instance.should_receive(:existing_method).and_return(1)208 eq(1)209 end210 it "fails if an instance is created but no invocation occurs" do211 expect do212 klass.any_instance.should_receive(:existing_method)213 klass.new214 klass.rspec_verify215 raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)216 end217 it "fails if no instance is created" do218 expect do219 klass.any_instance.should_receive(:existing_method)220 klass.rspec_verify221 raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)222 end223 it "fails if no instance is created and there are multiple expectations" do224 expect do225 klass.any_instance.should_receive(:existing_method)226 klass.any_instance.should_receive(:another_existing_method)227 klass.rspec_verify228 raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: another_existing_method, existing_method')229 end230 context "after any one instance has received a message" do231 it "passes if subsequent invocations do not receive that message" do232 klass.any_instance.should_receive(:existing_method)233 klass.new235 end236 it "fails if the method is invoked on a second instance" do237 instance_one = klass.new238 instance_two = klass.new239 expect do240 klass.any_instance.should_receive(:existing_method)241 instance_one.existing_method242 instance_two.existing_method243 raise_error(RSpec::Mocks::MockExpectationError, "The message 'existing_method' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")244 end245 end246 end247 context "message count" do248 context "the 'once' constraint" do249 it "passes for one invocation" do250 klass.any_instance.should_receive(:foo).once251 end253 it "fails when no instances are declared" do254 expect do255 klass.any_instance.should_receive(:foo).once256 klass.rspec_verify257 raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)258 end259 it "fails when an instance is declared but there are no invocations" do260 expect do261 klass.any_instance.should_receive(:foo).once262 klass.new263 klass.rspec_verify264 raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)265 end266 it "fails for more than one invocation" do267 expect do268 klass.any_instance.should_receive(:foo).once269 instance = klass.new270 2.times { }271 instance.rspec_verify272 raise_error(RSpec::Mocks::MockExpectationError)273 end274 end275 context "the 'twice' constraint" do276 it "passes for two invocations" do277 klass.any_instance.should_receive(:foo).twice278 instance = klass.new279 2.times { }280 end281 it "fails for more than two invocations" do282 expect do283 klass.any_instance.should_receive(:foo).twice284 instance = klass.new285 3.times { }286 instance.rspec_verify287 raise_error(RSpec::Mocks::MockExpectationError)288 end289 end290 context "the 'exactly(n)' constraint" do291 it "passes for n invocations where n = 3" do292 klass.any_instance.should_receive(:foo).exactly(3).times293 instance = klass.new294 3.times { }295 end296 it "fails for n invocations where n < 3" do297 expect do298 klass.any_instance.should_receive(:foo).exactly(3).times299 instance = klass.new300 2.times { }301 instance.rspec_verify302 raise_error(RSpec::Mocks::MockExpectationError)303 end304 it "fails for n invocations where n > 3" do305 expect do306 klass.any_instance.should_receive(:foo).exactly(3).times307 instance = klass.new308 4.times { }309 instance.rspec_verify310 raise_error(RSpec::Mocks::MockExpectationError)311 end312 end313 context "the 'at_least(n)' constraint" do314 it "passes for n invocations where n = 3" do315 klass.any_instance.should_receive(:foo).at_least(3).times316 instance = klass.new317 3.times { }318 end319 it "fails for n invocations where n < 3" do320 expect do321 klass.any_instance.should_receive(:foo).at_least(3).times322 instance = klass.new323 2.times { }324 instance.rspec_verify325 raise_error(RSpec::Mocks::MockExpectationError)326 end327 it "passes for n invocations where n > 3" do328 klass.any_instance.should_receive(:foo).at_least(3).times329 instance = klass.new330 4.times { }331 end332 end333 context "the 'at_most(n)' constraint" do334 it "passes for n invocations where n = 3" do335 klass.any_instance.should_receive(:foo).at_most(3).times336 instance = klass.new337 3.times { }338 end339 it "passes for n invocations where n < 3" do340 klass.any_instance.should_receive(:foo).at_most(3).times341 instance = klass.new342 2.times { }343 end344 it "fails for n invocations where n > 3" do345 expect do346 klass.any_instance.should_receive(:foo).at_most(3).times347 instance = klass.new348 4.times { }349 instance.rspec_verify350 raise_error(RSpec::Mocks::MockExpectationError)351 end352 end353 context "the 'never' constraint" do354 it "passes for 0 invocations" do355 klass.any_instance.should_receive(:foo).never356 klass.rspec_verify357 end358 it "fails on the first invocation" do359 expect do360 klass.any_instance.should_receive(:foo).never361 raise_error(RSpec::Mocks::MockExpectationError)363 end364 context "when combined with other expectations" do365 it "passes when the other expecations are met" do366 klass.any_instance.should_receive(:foo).never367 klass.any_instance.should_receive(:existing_method).and_return(5)368 eq(5)369 end370 it "fails when the other expecations are not met" do371 expect do372 klass.any_instance.should_receive(:foo).never373 klass.any_instance.should_receive(:existing_method).and_return(5)374 klass.rspec_verify375 raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)376 end377 end378 end379 context "the 'any_number_of_times' constraint" do380 it "passes for 0 invocations" do381 klass.any_instance.should_receive(:foo).any_number_of_times382 end384 it "passes for a non-zero number of invocations" do385 klass.any_instance.should_receive(:foo).any_number_of_times386 instance = klass.new387 instance.foo388 instance.rspec_verify389 end390 it "does not interfere with other expectations" do391 klass.any_instance.should_receive(:foo).any_number_of_times392 klass.any_instance.should_receive(:existing_method).and_return(5)393 eq(5)394 end395 context "when combined with other expectations" do396 it "passes when the other expecations are met" do397 klass.any_instance.should_receive(:foo).any_number_of_times398 klass.any_instance.should_receive(:existing_method).and_return(5)399 eq(5)400 end401 it "fails when the other expecations are not met" do402 expect do403 klass.any_instance.should_receive(:foo).any_number_of_times404 klass.any_instance.should_receive(:existing_method).and_return(5)405 klass.rspec_verify406 raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)407 end408 end409 end410 end411 end412 context "when resetting post-verification" do413 let(:space) { }414 context "existing method" do415 before(:each) do416 space.add(klass)417 end418 context "with stubbing" do419 before(:each) do420 klass.any_instance.stub(:existing_method).and_return(1)421 klass.method_defined?(:__existing_method_without_any_instance__).should be_true422 end423 it "restores the class to its original state after each example when no instance is created" do424 space.verify_all425 klass.method_defined?(:__existing_method_without_any_instance__).should be_false426 eq(existing_method_return_value)427 end428 it "restores the class to its original state after each example when one instance is created" do429 space.verify_all431 klass.method_defined?(:__existing_method_without_any_instance__).should be_false432 eq(existing_method_return_value)433 end434 it "restores the class to its original state after each example when more than one instance is created" do435 space.verify_all438 klass.method_defined?(:__existing_method_without_any_instance__).should be_false439 eq(existing_method_return_value)440 end441 end442 context "with expectations" do443 context "ensures that the subsequent specs do not see expectations set in previous specs" do444 context "when the instance created after the expectation is set" do445 it "first spec" do446 klass.any_instance.should_receive(:existing_method).and_return( end449 it "second spec" do450 eq(existing_method_return_value)451 end452 end453 context "when the instance created before the expectation is set" do454 before :each do455 @instance = klass.new456 end457 it "first spec" do458 klass.any_instance.should_receive(:existing_method).and_return( @instance.existing_method460 end461 it "second spec" do462 @instance.existing_method.should eq(existing_method_return_value)463 end464 end465 end466 it "ensures that the next spec does not see that expectation" do467 klass.any_instance.should_receive(:existing_method).and_return( space.verify_all470 eq(existing_method_return_value)471 end472 end473 end474 context "with multiple calls to any_instance in the same example" do475 it "does not prevent the change from being rolled back" do476 klass.any_instance.stub(:existing_method).and_return(false)477 klass.any_instance.stub(:existing_method).and_return(true)478 klass.rspec_verify479 respond_to(:existing_method)480 eq(existing_method_return_value)481 end482 end483 it "adds an class to the current space when #any_instance is invoked" do484 klass.any_instance485 RSpec::Mocks::space.send(:mocks).should include(klass)486 end487 it "adds an instance to the current space when stubbed method is invoked" do488 klass.any_instance.stub(:foo)489 instance = klass.new490 instance.foo491 RSpec::Mocks::space.send(:mocks).should include(instance)492 end493 end494 end495 end496end...

Full Screen

Full Screen


Source:receive_spec.rb Github


Full Screen

...7 end9 shared_examples_for "a receive matcher" do |*options|10 it 'allows the caller to configure how the subject responds' do11 receive(:foo).and_return(5)12 expect( eq(5)13 end14 it 'allows the caller to constrain the received arguments' do15 receive(:foo).with(:a)16 expect {18 }.to raise_error(/received :foo with unexpected arguments/)20 end21 it 'allows a `do...end` block implementation to be provided' do22 receive(:foo) do23 424 end25 expect( eq(4)26 end27 it 'allows chaining off a `do...end` block implementation to be provided' do28 receive(:foo) do29 430 end.and_return(6)31 expect( eq(6)32 end33 it 'allows a `{ ... }` block implementation to be provided' do34 receive(:foo) { 5 }35 expect( eq(5)36 end37 it 'gives precedence to a `{ ... }` block when both forms are provided ' +38 'since that form actually binds to `receive`' do39 receive(:foo) { :curly } do40 :do_end41 end42 expect( eq(:curly)43 end44 it 'does not support other matchers', :unless => options.include?(:allow_other_matchers) do45 expect {46 eq(3)47 }.to raise_error(UnsupportedMatcherError)48 end49 end50 shared_examples_for "an expect syntax allowance" do |*options|51 include_examples "a receive matcher", *options52 it 'does not expect the message to be received' do53 receive(:foo)54 expect { verify_all }.not_to raise_error55 end56 end57 shared_examples_for "an expect syntax negative allowance" do58 it 'is disabled since this expression is confusing' do59 expect {60 wrapped.not_to receive(:foo)61 }.to raise_error(/not_to receive` is not supported/)62 expect {63 wrapped.to_not receive(:foo)64 }.to raise_error(/to_not receive` is not supported/)65 end66 end67 shared_examples_for "an expect syntax expectation" do |*options|68 include_examples "a receive matcher", *options69 it 'sets up a message expectation that passes if the message is received' do70 receive(:foo)71 receiver.foo72 verify_all73 end74 it 'sets up a message expectation that fails if the message is not received' do75 receive(:foo)76 expect {77 verify_all78 }.to raise_error(RSpec::Mocks::MockExpectationError)79 end80 it "reports the line number of expectation of unreceived message", :pending => options.include?(:does_not_report_line_num) do81 expected_error_line = __LINE__; receive(:foo)82 expect {83 verify_all84 }.to raise_error { |e|85 expect(e.backtrace.first).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)86 }87 end88 end89 shared_examples_for "an expect syntax negative expectation" do90 it 'sets up a negaive message expectation that passes if the message is not received' do91 wrapped.not_to receive(:foo)92 verify_all93 end94 it 'sets up a negative message expectation that fails if the message is received' do95 wrapped.not_to receive(:foo)96 expect {97 receiver.foo98 }.to raise_error(/expected: 0 times.*received: 1 time/m)99 end100 it 'supports `to_not` as an alias for `not_to`' do101 wrapped.to_not receive(:foo)102 expect {103 receiver.foo104 }.to raise_error(/expected: 0 times.*received: 1 time/m)105 end106 it 'allows the caller to constrain the received arguments' do107 wrapped.not_to receive(:foo).with(:a)108 def receiver.method_missing(*a); end # a poor man's stub...109 expect {110 }.not_to raise_error112 expect {113 }.to raise_error(/expected: 0 times.*received: 1 time/m)115 end116 it 'prevents confusing double-negative expressions involving `never`' do117 expect {118 wrapped.not_to receive(:foo).never119 }.to raise_error(/trying to negate it again/)120 expect {121 wrapped.to_not receive(:foo).never122 }.to raise_error(/trying to negate it again/)123 end124 end125 describe "allow(...).to receive" do126 include_examples "an expect syntax allowance" do127 let(:receiver) { double }128 let(:wrapped) { allow(receiver) }129 end130 end131 describe "allow(...).not_to receive" do132 include_examples "an expect syntax negative allowance" do133 let(:wrapped) { allow(double) }134 end135 end136 describe "allow_any_instance_of(...).to receive" do137 include_examples "an expect syntax allowance" do138 let(:klass) { }139 let(:wrapped) { allow_any_instance_of(klass) }140 let(:receiver) { }141 end142 end143 describe "allow_any_instance_of(...).not_to receive" do144 include_examples "an expect syntax negative allowance" do145 let(:wrapped) { allow_any_instance_of( }146 end147 end148 describe "expect(...).to receive" do149 include_examples "an expect syntax expectation", :allow_other_matchers do150 let(:receiver) { double }151 let(:wrapped) { expect(receiver) }152 end153 end154 describe "expect_any_instance_of(...).to receive" do155 include_examples "an expect syntax expectation", :does_not_report_line_num do156 let(:klass) { }157 let(:wrapped) { expect_any_instance_of(klass) }158 let(:receiver) { }159 end160 end161 describe "expect(...).not_to receive" do162 include_examples "an expect syntax negative expectation" do163 let(:receiver) { double }164 let(:wrapped) { expect(receiver) }165 end166 end167 describe "expect_any_instance_of(...).not_to receive" do168 include_examples "an expect syntax negative expectation" do169 let(:klass) { }170 let(:wrapped) { expect_any_instance_of(klass) }171 let(:receiver) { }172 end173 end174 shared_examples "using rspec-mocks in another test framework" do175 it 'can use the `expect` syntax' do176 dbl = double177 do178 expect(dbl).to receive(:foo).and_return(3)179 end180 expect( eq(3)181 end182 it 'expects the method to be called when `expect` is used' do183 dbl = double184 do185 expect(dbl).to receive(:foo)186 end187 expect { verify dbl }.to raise_error(RSpec::Mocks::MockExpectationError)188 end189 it 'supports `expect(...).not_to receive`' do190 dbl = double191 do192 expect(dbl).not_to receive(:foo)193 end194 expect { }.to raise_error(RSpec::Mocks::MockExpectationError)195 end196 it 'supports `expect(...).to_not receive`' do197 dbl = double198 do199 expect(dbl).to_not receive(:foo)200 end201 expect { }.to raise_error(RSpec::Mocks::MockExpectationError)202 end203 end204 context "when used in a test framework without rspec-expectations" do205 let(:framework) do206 do207 include RSpec::Mocks::ExampleMethods208 def eq(value)209 double("MyMatcher")210 end211 end212 end213 include_examples "using rspec-mocks in another test framework"214 it 'cannot use `expect` with another matcher' do215 expect {...

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.

Run Test-prof_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?