ruby - testing methods wrapped in blocks with RSpec -


in simplified example of i'm doing, let's have 2 calls database:

repo.add( something_stringy ) repo.remove( something_floaty ) 

and want use mocks database calls, real calls tested elsewhere:

let(:repo){   repo = double("repo")   repo.should_receive(:add).with(instance_of(string))   repo.should_receive(:remove).with(instance_of(float))   repo }  before { fakeklass.const_set :repo, repo } 

that's fine , dandy, if wrap calls in transaction i'm bit stumped:

repo.transaction   # ... error checking in here somewhere...   repo.add( something_stringy )   repo.remove( something_floaty ) end 

because if write mock receives transaction receive call, in block won't called, , get:

expected: 1 time received: 0 times

for of other mocks. able show me how should writing spec deal this? i've tried reading relevant page in rspec book on around(:each) clear mud me.

any appreciated.

you can use #and_yield yield expectation chain:

repo.should_receive( :transaction ).and_yield 

you don't need set double stub out methods on repo class. example, setup written:

before( :each )     repo.should_receive( :transaction ).and_yield     repo.should_receive( :add ).with( instance_of(string) )     repo.should_receive( :remove ).with( instance_of(float) ) end 

you might consider using stub instead of should_receive, doesn't set expectations:

before( :each )     repo.stub( :transaction ).and_yield     repo.stub( :add )     repo.stub( :remove ) end 

in general, should use should_receive when want test interaction between 2 objects. own personal rule of thumb if appears in before, use stub; if it's in example, specific value, use should_receive.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -