提前感谢!Sidekiq工作得很好,但是我不能用Devise测试它,或者我应该说我不能测试后者吗?
根据Sidekiq的文档,当测试模式设置为假的时候,给工人的任何作业都会被推送到一个名为jobs的数组中。因此,测试这个数组的增加是非常简单的。
但是,尽管它的后端包含了Sidekiq::Worker,但使用Design异步时,它并不是那么简单。下面是我试着测试的一小部分内容:
Devise::Async::Backend::Sidekiq.jobsDevise::Mailer.deliveriesActionMailer::Base.deliveriesDevise::Async::Backend::Worker.jobs这些测试对象中没有一个能显示出尺寸的增加。因为设计公司以模型回调的形式发送电子邮件,所以我尝试在模型和控制器规范中进行测试。使用“工厂女孩”和“数据库清理”,我还尝试了两种模式:事务模式和截断模式。不用说,我也尝试了两种模式的Sidekiq:假的!还有内联!.
我遗漏了什么?
发布于 2015-07-08 13:33:51
正如在文档中提到的,您可以将队列大小检查为
Sidekiq::Extensions::DelayedMailer.jobs.size发布于 2016-05-11 02:25:50
在这个问题上,偶然发现了一个由gitlab完成的漂亮的实现,我认为这可能有助于测试通过sidekiq队列推送的设备-异步或电子邮件。helper.rb helpers.rb
通过在spec_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加入/spec/support/email_helpers.rb
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。
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).flatten.count(user.email) == 1检查以查看ActionMailer::Base.deliveries是否正在传递给user.emailhttps://stackoverflow.com/questions/27068028
复制相似问题