2

Is it possible to check what arguments is being passed to a 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.calling(number) end end class B def self.calling(argument) # This code in this class is already testet end end 

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

1 Answer 1

2

if you're using rspec mock/stubs try

B.should_receive(calling).with(10) 

http://kerryb.github.com/iprug-rspec-presentation/ covers a lot of basic usage, or see the docs or rspec book.

Sign up to request clarification or add additional context in comments.

2 Comments

should_receive is an extension to object (I think) so it works on classes and instances just the same. It works by stubbing out the method with some code that tracks whether it's called or not. So you'd set up your object instance and call should receive on that in the same way. Can also look at different kinds of matchers (e.g. argument is a string, integer, even number, …), times called, ordering of different calls to the same method on the same object, etc.
I think to test calling on an instance you would need to set the should_receive on an instance of that class. You might also be interested in mock_model and stub_model

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.