我正在使用Devise來管理用戶。它在開發模式下工作,但不在測試中。在開發中,調用User.invite!成功發送邀請。在測試中,它會自動失敗 - 沒有錯誤,所有與邀請相關的字段都只有一個零值,並且郵件計數不會增加。設計可邀請不會在測試環境中創建邀請
這裏是我的測試樣子(使用FactoryGirl和RSpec)
require "rails_helper"
describe "with inviting user" do
let(:user) { create(:user) }
before(:each) do
Devise.mailer.deliveries = []
User.invite!({email: user.email}, user)
end
after(:each) do
Devise.mailer.deliveries.clear
end
it 'should generate invite token' do
expect(user.invitation_token).to_not be_nil
end
it 'should send an email' do
expect(Devise.mailer.deliveries.count).to eq(1)
end
end
兩個測試失敗:
1) with inviting user should generate invite token
Failure/Error: expect(user.invitation_token).to_not be_nil
expected: not nil
got: nil
# ./spec/mailer/mailer_spec.rb:20:in `block (2 levels) in <top (required)>'
2) with inviting user should send an email
Failure/Error: expect(Devise.mailer.deliveries.count).to eq(1)
expected: 1
got: 0
(compared using ==)
# ./spec/mailer/mailer_spec.rb:28:in `block (2 levels) in <top (required)>'
我有點爲難。有任何想法嗎?
只是FYI我也在config/environments/test.rb中設置了config.action_mailer.perform_deliveries = true – nilatti
我認爲你也有這個'config.action_mailer.delivery_method =:test'? –
是的,這是默認設置:config.action_mailer.delivery_method =:test – nilatti