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

Jared picture Jared · Aug 11, 2011 · Viewed 7k times · Source

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?

Answer

Anh Nguyen picture Anh Nguyen · Apr 16, 2012

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)