使用 Devise 的可确认模块进行测试 (RSpec) [英] Testing (RSpec) with Devise's confirmable module
问题描述
我的理解是 Rails 的测试环境在每次测试之前都被拆除并重建......那么我如何测试一个需要用户登录的控制器并且如果没有设备的可确认模块就无法创建该用户在路上?
It's my understanding that Rails' testing environment is torn down and rebuilt before each test...so how do I test a controller that requires that a user be logged in and that user can't be created without Device's confirmable module getting in the way?
Devise 的推荐方法(如下) 创建一个新用户,然后由 Devise 的 confirmable
模块发送一封电子邮件.我如何解决这个问题,这样我就不会每次都创建"一个用户……或者如果我是的话,我可以获得一个对象来测试,而无需模拟"每个规范的新电子邮件?
Devise's recommended method (below) creates a new user which is then sent an email by Devise's confirmable
module. How do I get around this so I'm not 'creating' a user each time...or if I am I can get an object to test w/out "simulating" a new email for each spec?
before(:each) do
@user = Factory.create(:user)
sign_in @user
end
我确定我忽略了一些非常明显的东西,因为对于使用 Devise 的任何人来说,这一定是一个非常常见的规范......
I'm sure I'm overlooking something painfully obvious as this must be a very common spec for anyone using Devise with confirmable...
推荐答案
在你的测试环境中 ActionMailer::Base.delivery_method
应该设置为 :test
,这意味着这些电子邮件将不会被发送出去.如果通过 config/environments.rb
中的配置将此设置设置为其他内容,例如 smtp
,则将发送电子邮件将出来.
In your test environment ActionMailer::Base.delivery_method
should be set to :test
, which means that these emails will not be sent out. If this setting is set to something else such as smtp
by way of a configuration in say config/environments.rb
, then emails will be sent out.
如果该设置已经存在,那么要使用 User
对象(如实际能够登录),您需要调用 confirm!
on它:
If that setting's already there, then to use the User
object (as in, to be actually able to log in) you'll need to call confirm!
on it:
user = User.first
user.confirm!
这篇关于使用 Devise 的可确认模块进行测试 (RSpec)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!