On Thursday, March 5, 2015 at 7:06:58 PM UTC-8, Joe Van Dyk wrote:
>
> Say I have the following method:
>
>       def run
>         transaction do
>           mark_non_eligible
>           make_invoice_batch
>           send_batch_email
>         end
>       end
>
> How can I test that with rspec's mocks?
>
> Joe
>

There's a rarely-used feature of `and_yield` that can help you with this. 
 If you pass a block to `and_yield`, RSpec will pass your block an object 
that it will use to instance_eval the `transaction` block, allowing you to 
set message expectations on it.  I put together an example gist:

https://gist.github.com/myronmarston/e79cbff12ce51b54814b

This works, but bear in mind there are a number of code smells here 
(mocking the object-under test, making a test that simply mirrors the 
implementation, etc).  IMO, you'd be better off testing this with an 
integrated test that hits the DB.  You could, for example, stub whatever 
collaborator `send_batch_email` delegates to so that it fails, and check 
that the database updates performed by `mark_non_eligible` and 
`make_invoice_batch` are rolled back.

HTH,
Myron

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/a22807a5-5090-4e5c-bfd7-a3ec7cf4b25a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to