I have defined the following method:
def some_method
x = x + 1
y = some_other_method(x)
x + y
end
Now in my rspec spec, can I mock the call to some_other_method for my unit test for some_method?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can indeed mock out other methods in a RSpec test. If the two methods you mentioned are inside a class,
Foo, you would do something like this to make sure thatsome_other_methodis called:If you don’t need to assert that is was called, just assert the results of
some_method, you can do something like this instead:The above examples assume you’re using RSpec 2. If you’re using RSpec 1, you’ll need to use
stubsinstead ofstub.If your methods are defined outside of a class, they’re really defined on the class
Object, so just useObjectinstead ofFooin the examples above.For more information about mocks in RSpec, check out http://relishapp.com/rspec/rspec-mocks for RSpec 2 or http://rspec.info/documentation/mocks/ for RSpec 1.