Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 如何用Sidekiq测试设计异步?_Ruby On Rails 4_Devise_Rspec Rails_Sidekiq_Devise Async - Fatal编程技术网

Ruby on rails 4 如何用Sidekiq测试设计异步?

Ruby on rails 4 如何用Sidekiq测试设计异步?,ruby-on-rails-4,devise,rspec-rails,sidekiq,devise-async,Ruby On Rails 4,Devise,Rspec Rails,Sidekiq,Devise Async,提前谢谢! Sidekiq工作得很好,但我无法用Desive Async测试它,或者说我不能测试后者 根据Sidekiq的文档,当测试模式设置为fake!,给工人的任何作业都被推送到同一工人的名为jobs的数组中。因此,测试这个数组的增加是很简单的 但是,使用Deviate Async,它并不是那么简单,尽管它的后端包括Sidekiq::Worker。下面是我尝试测试的一小部分内容: designe::Async::Backend::Sidekiq.jobs designe::Mailer.d

提前谢谢! Sidekiq工作得很好,但我无法用Desive Async测试它,或者说我不能测试后者

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

但是,使用Deviate Async,它并不是那么简单,尽管它的后端包括
Sidekiq::Worker
。下面是我尝试测试的一小部分内容:

  • designe::Async::Backend::Sidekiq.jobs
  • designe::Mailer.deliveries
  • ActionMailer::Base.deliveries
  • designe::Async::Backend::Worker.jobs
这些受试者中没有一人的体型有所增加。由于Deviate以模型回调的形式发送电子邮件,我尝试在模型和控制器规范中进行测试。使用Factory Girl和数据库清理器,我还尝试了两种模式:事务和截断。不用说,我也尝试了Sidekiq的两种模式:假的!还有内联

我遗漏了什么?

如中所述,您可以检查队列大小,如下所示:

Sidekiq::Extensions::DelayedMailer.jobs.size

当时正在研究这个问题,偶然发现gitlab完成了一个漂亮的实现,我认为这可能有助于测试Desive async或电子邮件,它们通过sidekiq队列推送。

通过在
spec\u helper.rb

# 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
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
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
并添加
/spec/support/email\u helpers.rb

# 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
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
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
为了运行测试,例如测试您忘记的密码,我假设您知道rspec、factorygirl、水豚
/spec/features/password\u reset\u spec.rb

# 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
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
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
您会注意到在这个测试实现中

  • 将导致sidekiq运行作业,而不是将其排队
  • 用户模型电子邮件属性必须被称为
    email
    ,或者您可以替换上面的代码

  • ActionMailer::Base.deliveries.map(&:to).flant.count(user.email)==1
    检查以查看ActionMailer::Base.deliveries正在传递给user.email

  • ActionMailer::Base.deliveries在我使用sidekiq时是[]吗?我们需要为此做什么配置