Rails和Devise的功能测试。什么放在我的装置? [英] Functional testing with Rails and Devise. What to put in my fixtures?
问题描述
在我的用户模型中,我的用户年龄,我要测试用户只能访问某个页面,如果它们是:
- 登录
- 超过35
我在Devise文档中看到我可以使用:* sign_in *,我把它放在我的测试中它似乎工作 - 测试没有错误,因为我有:
include Devise :: TestHelpers
在我的* test_helper.rb *
当我拿出我的测试错误,因为* sign_in *没有定义。所以这不是一个帮助问题。
当我运行测试并检查是否有一个事件发生时,测试失败,因为有0次发生。如果user_signed_in?*
需要放入我的灯具或测试来创建一个测试用户,还有一个完全注册的用户(确认等)?
查看代码:
<%如果user_signed_in? %GT;
< span id =loggedin>用户已登录< / span>
用户年龄为< span id =age><%= current_user.age.to_s%>< / span>
<%end%>
测试代码:
test应该得到索引do
sign_in:one
get:index
assert_response:success
assert_select'span#loggedin',:count => 1
end
夹具:
一个:
电子邮件:jez@example.com
年龄:36
当我手动登录时,它在开发中工作正常,但我希望自动化这一切 - 真正的测试点!
解决方案Devise pre 3.2.0
我认为这可能是你正在寻找:
User.new.send(:password_digest,'password')
/ pre>
当盐没有时,它工作。
所以在你的夹具中你可以这样做:
one:
电子邮件:'some@user.com'
encrypted_password:<%=用户.new.send(:password_digest,'password')%>
Devise版本3.2.0的解决方案到3.5.0
在Devise 3.2.0中,方法是为此而精确地创建的。(@Inkling更新)。对于这些版本,encrypted_password应该如下定义:
encrypted_password:<%= Devise.bcrypt(User,'password' )%>
其中
用户
是您的用户的类
请注意,这仅适用于您使用默认加密算法(bcrypt)。
Devise版本3.5.1及更高版本的解决方案
Katarzyna 已经指出:diff-19b4dd928714c72f1338874351e8ff2d =noreferrer>从该版本开始,encrypted_password应该如下定义:
encrypted_password:<%= Devise :: Encryptor.digest用户,'密码')%>
Hi I'm wanting to do some functional testing of my Rails 3 app that uses Devise and CanCan.
In my User model I have the users age, I want to test that a user can only visit a certain page if they are:
- Logged in
- Over 35
I have seen in the Devise document that I can use: *sign_in* and I have put it in my tests and it appears to work - the test doesn't error because I have:
include Devise::TestHelpers
in my *test_helper.rb*
When I take it out my test does error because *sign_in* is not defined. So it is not a helper problem.
When I run the test and check to see if span#loggedin has one occurrence the test fails because there is 0 occurrences. span#loggedin only appears *if user_signed_in?*
What do I need to put in my fixtures or tests to create a test user who is also a fully signed up user (confirmed etc)?
View Code:
<% if user_signed_in? %> <span id="loggedin">User is signed in</span> User age is <span id="age"><%= current_user.age.to_s %></span> <% end %>
Test Code:
test "should get index" do sign_in :one get :index assert_response :success assert_select 'span#loggedin', :count => 1 end
Fixture:
one: email: jez@example.com age: 36
It works okay in development when I manually login but I'm hoping to automate it all - the point of testing really!!
解决方案Solution for Devise pre 3.2.0
I think this might be what you are looking for:
User.new.send(:password_digest, 'password')
It works when the salt is nil.
And therefore in your fixture you can do this:
one: email: 'some@user.com' encrypted_password: <%= User.new.send(:password_digest, 'password') %>
Solution for Devise versions 3.2.0 to 3.5.0
At Devise 3.2.0, a method was created precisely for this purpose (@Inkling update). For these versions the encrypted_password should be defined like this:
encrypted_password: <%= Devise.bcrypt(User, 'password') %>
where
User
is the class of your user model.Note that this only applies if you're using the default encryption algorithm (bcrypt).
Solution for Devise versions 3.5.1 and above
As Katarzyna has pointed out: Device.bcrypt has been deprecated in version 3.5.1. From that version on, the encrypted_password should be defined like this:
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
这篇关于Rails和Devise的功能测试。什么放在我的装置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!