我的电子邮件确认控制器规范失败

时间:2016-09-06 12:44:20

标签: ruby-on-rails rspec capybara

此处我测试电子邮件确认失败。在开发模式下一切正常。而且我认为我错过了一些配置。

Failure/Error: expect{ post :create, authorization: { email: "user@email.com" } }.to change(ActionMailer::Base.deliveries, :count).by(1)
   expected #count to have changed by 1, but was changed by 0

错误

context 'with valid provider data' do
  before { session["devise.provider_data"] = { provider: 'twitter', uid: '123456' } }
  before(:each) do
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
  end

  after(:each) do
    ActionMailer::Base.deliveries.clear
  end

  it "sends email confirmation" do
    expect{ post :create, authorization: { email: "user@email.com" } }.to change(ActionMailer::Base.deliveries, :count).by(1)
  end
end

Coul有人帮忙吗?

Readability.parse(url: articleUrl, { data in
  let title = data?.title
  let description = data?.description
  let keywords = data?.keywords
  let imageUrl = data?.topImage
  let videoUrl = data?.topVideo
})

1 个答案:

答案 0 :(得分:2)

您缺少一些配置。我通常在测试电子邮件行为的测试之前在回调之前和之后进行设置,例如:

before(:each) do
  ActionMailer::Base.delivery_method = :test
  ActionMailer::Base.perform_deliveries = true
  ActionMailer::Base.deliveries = []
end

after(:each) do
  ActionMailer::Base.deliveries.clear
end

如果您希望上述配置适用于您的所有测试套件,您可以将其包含在配置块中的spec/spec_helper.rb文件中:

RSpec.configure do |config|
  ....      

  config.before(:suite) do
      ActionMailer::Base.delivery_method = :test
      ActionMailer::Base.perform_deliveries = true
      ActionMailer::Base.deliveries = []
  end

  config.after(:suite) do
      ActionMailer::Base.deliveries.clear
  end
end