ruby-on-rails - 使用 rSpec 测试 delayed_job 链的最佳方法是什么?

标签 ruby-on-rails ruby rspec delayed-job

目前,当我的代码中有一个延迟方法时,如下所示:

CommentMailer.delay.deliver_comments(@comment, true)

我在规范中写了这样的东西:

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

一般来说,有没有更好的方法来处理这个和/或类似 rSpec 中的链式方法?

最佳答案

我们可以在 before block 中再增加一行,如下所示:

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

然后您就可以进行正常的模拟检查,如下所示:

CommentMailer.should_receive(:deliver_comments).with(comment, true)

关于ruby-on-rails - 使用 rSpec 测试 delayed_job 链的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7030974/

相关文章:

Ruby selenium/capybara 等待元素消失

ruby-on-rails - 使用 RSpec 测试 Controller 时如何调用 Rails 命名路由?

ruby-on-rails - Rails 3 - 文件名太长错误

ruby - Nginx 配置上的 Sinatra - 出了什么问题?

ruby-on-rails - Active Record 如何添加记录到 has_many :through association in rails

ruby - 平衡括号的简单 Ragel 示例?

ruby - 数组用逗号连接,仅当元素不为 nil 时

ruby-on-rails - 弃用警告:ActionController::TestCase HTTP 请求方法将只接受关键字参数

ruby-on-rails - 表单中的第一个参数不能包含 nil 或在 rails 4 中为空

ruby-on-rails - 如何在Rails的 “content_tag”属性中使用HTML实体?