2011-02-05 78 views
2

当使用rspec进行测试时,是否可以检查传递给非静态方法的参数?在Rspec中测试非静态方法?

如果我即将要测试A类,那么在A类内部我称之为B类,B已经过测试。我想测试的唯一的事是迁入参数B.

class A 
    def method 
    number = 10 
    b = B.new 
    b.calling(number) 
    end 
end 

class B 
    def calling(argument) 
    # This code in this class is already testet 
    end 
end 

如何测试的进入参数b.calling

我已经尝试过目前为止没有成功。

it "should work" do 
    b = mock(B) 
    b.should_receive(:calling).at_least(1).times 
    A.new.method 
end 

它总是失败,怎么一回事,因为b从来没有被调用。

回答

4

在规范的B不是B A被实例化(返回,当你调用新的,因为你还没有存根其B的真实实例),试试这个:

it "should work" do 
    b = mock(B) 
    B.should_receive(:new).and_return(b) 
    b.should_receive(:calling).at_least(1).times 
    A.new.method 
end 
+0

谢谢,这成功了! – Oleander 2011-02-05 20:04:49