首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >devise-async & sidekiq - RSpec集成规范失败

devise-async & sidekiq - RSpec集成规范失败
EN

Stack Overflow用户
提问于 2013-05-24 17:45:57
回答 1查看 987关注 0票数 1

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

代码语言:javascript
复制
# 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

和:

代码语言:javascript
复制
# 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来自以下模块:

代码语言:javascript
复制
module MailerMacros
  def last_email
    ActionMailer::Base.deliveries.last
  end

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

当在用户模型上禁用devise-async时,所有这三个规范都工作得很好。当我打开它时,密码重置规范运行正常,但是寄存器1抱怨last_email为空,我不明白为什么。发送的确认邮件与密码重置邮件有什么不同吗?

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

EN

回答 1

Stack Overflow用户

发布于 2013-05-24 23:24:46

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

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

下面是我需要修改的内容:

在我的Gemfile中添加gem 'database_cleaner'

明显修改spec_helper.rb

代码语言:javascript
复制
config.use_transactional_fixtures = false

将以下内容添加到spec_helper.rb

代码语言:javascript
复制
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中重做我的代码块以读取:

代码语言:javascript
复制
describe "Registers" do
  it "should inform the user to confirm account", with_transaction_callbacks: true do
    [ --- 8< snip --- ]
  end
end

魔术发生在第二行。

PS。This堆栈溢出主题以及从它内部链接的the article也有帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16732060

复制
相关文章

相似问题

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