Ruby on rails 4 如何用Sidekiq测试设计异步?
提前谢谢! Sidekiq工作得很好,但我无法用Desive Async测试它,或者说我不能测试后者 根据Sidekiq的文档,当测试模式设置为fake!,给工人的任何作业都被推送到同一工人的名为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
jobs
的数组中。因此,测试这个数组的增加是很简单的
但是,使用Deviate Async,它并不是那么简单,尽管它的后端包括Sidekiq::Worker
。下面是我尝试测试的一小部分内容:
designe::Async::Backend::Sidekiq.jobs
designe::Mailer.deliveries
ActionMailer::Base.deliveries
designe::Async::Backend::Worker.jobs
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
您会注意到在这个测试实现中
email
,或者您可以替换上面的代码ActionMailer::Base.deliveries.map(&:to).flant.count(user.email)==1
检查以查看ActionMailer::Base.deliveries正在传递给user.emailActionMailer::Base.deliveries在我使用sidekiq时是[]吗?我们需要为此做什么配置