2015-12-15 73 views
0

如果我有像存根與塊一類作爲參數在構造

class MyClass 

def initialize(&block) 
    @myBlock = block 
end 

def process 
    #... 
    @myBlock.call 
    #.. 
end 

一個類和我想測試一DifferentClass其需要使用MyClass因而在new接收的塊。

我該如何對該塊進行存根?在這種情況下我能做些什麼?

回答

0
describe DifferentClass do 
    it 'zomfg' do 
    allow(MyClass).to receive(:new) do |&block| 
     expect(block).to receive(:call) 
     my_object = double 
     allow(my_object).to receive(:process) { block.call } 
     my_object 
    end 

    foo = MyClass.new {} 
    # Here you should be passing the foo object to 
    # whatever method DifferentClass uses it in 
    foo.process 
    end 
end 
相關問題