您好,我想对使用 Devise 和 CanCan 的 Rails 3 应用程序进行一些功能测试。
在我的用户模型中,我有用户年龄,我想测试用户只能访问某个页面,如果他们是:
- 登录
- 超过35
我在设计文档中看到我可以使用:*sign_in*并且我已经把它在我的测试中并且似乎有效 - 测试不会出错,因为我有:
include Devise::TestHelpers
在我的 *test_helper.rb* 中
当我把它拿出来时,我的测试会出错,因为 *sign_in* 未定义。所以这不是一个辅助问题。
当我运行测试并检查 span#loggedin 是否出现 1 次时,测试失败,因为出现 0 次。 span#loggedin 仅在 *if user_signed_in 时出现?*
我需要在固定装置或测试中添加什么来创建一个完全注册用户(已确认等)的测试用户?
查看代码:
<% 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 "should get index" do
sign_in :one
get :index
assert_response :success
assert_select 'span#loggedin', :count => 1
end
Fixture:
one:
email: [email protected]
age: 36
当我手动登录时,它在开发中工作正常,但我希望将其全部自动化 - 真正的测试点!
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: [email protected]
age: 36
It works okay in development when I manually login but I'm hoping to automate it all - the point of testing really!!
发布评论
评论(4)
Devise 3.2.0 之前的解决方案
我想这可能就是您正在寻找的:
它在盐为零时有效。
因此,在您的装置中,您可以执行以下操作:
Devise 版本 3.2.0 至 3.5.0 的解决方案
在 Devise 3.2.0 中,方法正是为此目的而创建的(@Inkling 更新)。对于这些版本,encrypted_password 应如下定义:
其中
User
是用户模型的类。请注意,这只适用于您使用默认加密算法 (bcrypt) 的情况。
Devise 版本 3.5.1 及更高版本的解决方案
As Katarzyna 指出:Device.bcrypt 在版本 3.5.1 中已被弃用。从该版本开始,encrypted_password 应如下定义:
Solution for Devise pre 3.2.0
I think this might be what you are looking for:
It works when the salt is nil.
And therefore in your fixture you can do this:
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:
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:
我已经解决了问题 - 我错误地登录了用户。
我的测试应该阅读:
这也有效并消除了一行代码:
我对夹具的理解缺乏......
但是回到问题 - 在夹具中放入什么:
现在它可以工作了。如果有更简单的方法可以在开发中生成用户并将数据粘贴到固定装置中,请发布。
I have sorted out the problem - I was logging in the user incorrectly.
My test should hare read:
This also works and eliminates a line of code:
My understanding of Fixtures was lacking ...
But back to the question - what to put in fixtures:
Now it works. If there is an easier that generating a user in dev and pasting the data into a fixture please do post.
只是还有一个提示。这对我来说非常有用:
http: //brandonhilkert.com/blog/managing-login-passwords-for-capybara-with-minitest-and-rails-fixtures/
这似乎是干燥且不错的解决方案。
Just one more hint. This worked great for me:
http://brandonhilkert.com/blog/managing-login-passwords-for-capybara-with-minitest-and-rails-fixtures/
And it seems to be DRY and nice solution.
它对我有用。
我没有更改用户设备上的任何内容,如下所示:
但我更改了
test_helper.rb
并将其添加到类ActionController
中(这适用于所有控制器测试):It's working for me.
I didn't change anything on my user fixture, it's like this:
But I changed my
test_helper.rb
and I added this to the classActionController
(this will work for all controllers tests):