Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to test delayed_job chains with rSpec?

Currently when I have a delayed method in my code like the following:

CommentMailer.delay.deliver_comments(@comment, true)

I write something like this in my spec:

dj = mock("DelayProxy")
CommentMailer.should_receive(:delay).and_return(dj)
dj.should_receive(:deliver_comments).with(comment, true)

Is there a better way to handle this and/or chained methods like that in rSpec in general?

like image 269
Jared Avatar asked Aug 11 '11 18:08

Jared


2 Answers

Here are some discussions about chaining methods in rSpec that I found helpful:

Stubbing Chained Methods with Rspec

http://groups.google.com/group/rspec/browse_thread/thread/6b8394836d2390b0#

like image 71
Jared Avatar answered Nov 14 '22 01:11

Jared


We can just have one more line in the before block as following:

CommentMailer.stub(:delay).and_return(CommentMailer)

Then you then can have the normal mock check as following:

CommentMailer.should_receive(:deliver_comments).with(comment, true)
like image 27
Anh Nguyen Avatar answered Nov 14 '22 00:11

Anh Nguyen