首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用Sidekiq测试设计异步?

如何用Sidekiq测试设计异步?
EN

Stack Overflow用户
提问于 2014-11-21 18:35:31
回答 2查看 927关注 0票数 8

提前感谢!Sidekiq工作得很好,但是我不能用Devise测试它,或者我应该说我不能测试后者吗?

根据Sidekiq的文档,当测试模式设置为假的时候,给工人的任何作业都会被推送到一个名为jobs的数组中。因此,测试这个数组的增加是非常简单的。

但是,尽管它的后端包含了Sidekiq::Worker,但使用Design异步时,它并不是那么简单。下面是我试着测试的一小部分内容:

  • Devise::Async::Backend::Sidekiq.jobs
  • Devise::Mailer.deliveries
  • ActionMailer::Base.deliveries
  • Devise::Async::Backend::Worker.jobs

这些测试对象中没有一个能显示出尺寸的增加。因为设计公司以模型回调的形式发送电子邮件,所以我尝试在模型和控制器规范中进行测试。使用“工厂女孩”和“数据库清理”,我还尝试了两种模式:事务模式和截断模式。不用说,我也尝试了两种模式的Sidekiq:假的!还有内联!.

我遗漏了什么?

EN

回答 2

Stack Overflow用户

发布于 2015-07-08 13:33:51

正如在文档中提到的,您可以将队列大小检查为

代码语言:javascript
复制
Sidekiq::Extensions::DelayedMailer.jobs.size
票数 1
EN

Stack Overflow用户

发布于 2016-05-11 02:25:50

在这个问题上,偶然发现了一个由gitlab完成的漂亮的实现,我认为这可能有助于测试通过sidekiq队列推送的设备-异步或电子邮件。helper.rb helpers.rb

通过在spec_helper.rb中添加这些行

代码语言:javascript
复制
# An inline mode that runs the job immediately instead of enqueuing it
require 'sidekiq/testing/inline'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
  config.include EmailHelpers
  # other configurations line
end

加入/spec/support/email_helpers.rb

代码语言:javascript
复制
module EmailHelpers
  def sent_to_user?(user)
    ActionMailer::Base.deliveries.map(&:to).flatten.count(user.email) == 1
  end

  def should_email(user)
    expect(sent_to_user?(user)).to be_truthy
  end

  def should_not_email(user)
    expect(sent_to_user?(user)).to be_falsey
  end
end

要运行测试(例如测试您忘记的密码),我假设您了解rspec、工厂女孩、capybara /spec/features/password_reset_spec.rb

代码语言:javascript
复制
require 'rails_helper'

feature 'Password reset', js: true do
  describe 'sending' do
    it 'reset instructions' do
      #FactoryGirl create
      user = create(:user)
      forgot_password(user)

      expect(current_path).to eq(root_path)
      expect(page).to have_content('You will receive an email in a few minutes')
      should_email(user)
    end
  end

  def forgot_password(user)
    visit '/user/login'
    click_on 'Forgot password?'
    fill_in 'user[email]', with: user.email
    click_on 'Reset my password'
    user.reload
  end
end

您会注意到在这个测试实现中

  1. 将导致sidekiq运行作业而不是对其进行排队,
  2. 用户模型电子邮件属性必须称为email,或者您只需替换上面的代码即可。
  3. ActionMailer::Base.deliveries.map(&:to).flatten.count(user.email) == 1检查以查看ActionMailer::Base.deliveries是否正在传递给user.email
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27068028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档