测试(RSpec)与Devise的可确认模块 [英] Testing (RSpec) with Devise's confirmable module

查看:140
本文介绍了测试(RSpec)与Devise的可确认模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,Rails的测试环境在每次测试之前被拆除和重建,所以如何测试需要用户登录的控制器,如果没有设备的可确认模块,则无法创建该用户在路上?



Devise的推荐方法(下图)创建一个新用户,然后由Devise的可确认模块发送电子邮件。如何解决这个问题,所以我不是每次都在创建一个用户,或者如果我可以得到一个对象来测试w / out模拟每个规范的新电子邮件?

  before(:each)do 
@user = Factory.create(:user)
sign_in @user
结束

我确定我忽略了一些痛苦的明显的东西,因为这必须是一个非常常见的规范任何使用Devise确认的人...

解决方案

在您的测试环境中 ActionMailer :: Base.delivery_method 应设置为:test ,这意味着这些电子邮件不会被发送出去。如果通过 config / environments.rb 中的配置,将此设置设置为其他诸如 smtp 那么电子邮件将被发送出去。



如果该设置已经存在,那么使用 User 对象(实际上可以登录),您需要调用确认!

  user = User.first 
user.confirm!


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'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

I'm sure I'm overlooking something painfully obvious as this must be a very common spec for anyone using Devise with confirmable...

解决方案

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.

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!

这篇关于测试(RSpec)与Devise的可确认模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆