开发者

Test non static methods in Rspec?

Is it possible to check what arguments is being passed to a non static method when testing with rspec?

If I for i.e want to test class A, inside class A i call class B, B is already tested. The only thing I want to test is the ingoing arguments to 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

How do I test the ingoing arguments to b.calling?

I've tried this so far, without success.

it "should work" do
  b = mock(B)
  b.should_receive(:calling).at_least(1).t开发者_如何学运维imes
  A.new.method
end

It always fails, beacuse b never was called.


the b in your spec isn't the b A is instantiating (it returns a real instance of B when you call new since you haven't stubbed it), try this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜