Be careful with stub! in RSpec

First of all, you should be careful with any call to stub.

You probably want to use stub just to simulate a behavior, one that you don’t want to test in your current test.

For example: You may want to use stub with a remote service, an API call.

Also, the method is stub not stub! (at least in RSpec 2.6.0)

 
This will not work but will not fail.

  # This won't work
  DineroMailIpn::Client.any_instance.stub!(
    :consulta_pago).and_return(@response_with_pays)

This is what you are looking for.

  # This will work
  DineroMailIpn::Client.any_instance.stub(
    :consulta_pago).and_return(@response_with_pays)

I don’t know exactly why stub! doesn’t fail, but it wasn’t doing what I was expecting (to actually stub that method and return the mock object I wanted)

This entry was posted in developer tools, Ruby programming, web programming and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free