【问题标题】:devise-async & sidekiq - RSpec integration spec failsdevise-async & sidekiq - RSpec 集成规范失败
【发布时间】:2013-05-24 23:31:41
【问题描述】:

我为我的应用程序的基于设计的身份验证编写了以下集成测试:

# password_resets_spec.rb

require 'spec_helper'

describe "PasswordResets" do
  it "emails user when requesting password reset" do
    user = FactoryGirl.create(:user)
    reset_email # or else we'll have the confirmation email in the last assertion
    visit new_user_session_path
    click_link "password"
    fill_in "Email", with: user.email
    click_button "Send"
    current_path.should eq(new_user_session_path)
    page.should have_content "Will receive"
    last_email.to.should include(user.email)
  end

  it "doesn't email invalid user when requesting password reset" do
    user = FactoryGirl.create(:user)
    reset_email # or else we'll have the confirmation email in the last assertion
    visit new_user_session_path
    click_link "password"
    fill_in "Email", with: 'nobody@example.com'
    click_button "Send"
    current_path.should eq(user_password_path)
    page.should have_content "correct"
    last_email.should be_nil
  end
end

和:

# registers_spec.rb

require 'spec_helper'

describe "Registers" do
  it "should inform the user to confirm account" do
    user = FactoryGirl.build(:user)
    visit new_user_registration_path
    fill_in "Username", with: user.username
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
    fill_in "Confirm password", with: user.password
    click_button "Send"
    current_path.should eq(root_path)
    page.should have_content "You have been sent"
    last_email.to.should include(user.email)
  end
end

我将 Sidekiq 用于后台作业,last_emailreset_email 来自以下模块:

module MailerMacros
  def last_email
    ActionMailer::Base.deliveries.last
  end

  def reset_email
    ActionMailer::Base.deliveries.clear
  end
end

在 User 模型上停用 devise-async 时,所有这三个规范都可以正常工作。当我打开它时,密码重置规范运行正常,但寄存器抱怨 last_email 为零,我不明白为什么。与密码重置邮件相比,发送的确认邮件是否有所不同?

请注意,我的 spec_helper.rb 文件中有 require 'sidekiq/testing/inline' 行,以便立即完成电子邮件发送,并且为我的测试环境设置了 config.action_mailer.delivery_method = :test,因此不会发生实际的电子邮件发送。

【问题讨论】:

  • 您是如何在 User 模型上停用 devise-async 的?
  • 呵呵,因为不记得了,只好自己去挖掘源代码。给你:在用户模型devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable, :async - 这是异步开启,如果你删除 :async 它是关闭的。
  • 感谢您的回答

标签: ruby-on-rails rspec devise capybara sidekiq


【解决方案1】:

我已经用 mhfs 的help 解决了这个问题。问题是我在spec_helper.rb 中将config.use_transactional_fixtures 设置为true,因此用户是在事务中创建的,并且从未调用过发送电子邮件的after_commit 挂钩。密码重置显然没有在事务中运行,这就是它们起作用的原因。

所以我不得不关闭 use_transactional_fixtures 并使用 database_cleaner 来保持我的数据库整洁。

这是我必须修改的:

gem 'database_cleaner' 添加到我的Gemfile

明显修改spec_helper.rb:

config.use_transactional_fixtures = false

将以下内容添加到spec_helper.rb

config.before(:each) do
  with_transaction_callbacks = example.metadata[:with_transaction_callbacks]
  if with_transaction_callbacks
    DatabaseCleaner.strategy = :truncation
  else
    DatabaseCleaner.strategy = :transaction
  end
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end

最后重做我在registers_spec.rb 中的块以阅读:

describe "Registers" do
  it "should inform the user to confirm account", with_transaction_callbacks: true do
    [ --- 8< snip --- ]
  end
end

魔法发生在第二行。

PS。 This Stack Overflow 主题以及其中链接的 the article 也有帮助。

【讨论】:

    猜你喜欢
    • 2012-11-16
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多