使用 Rails 和 Devise 进行功能测试。我的灯具中应该放什么?

发布于 2024-10-10 04:04:52 字数 1176 浏览 0 评论 0原文

您好,我想对使用 Devise 和 CanCan 的 Rails 3 应用程序进行一些功能测试。

在我的用户模型中,我有用户年龄,我想测试用户只能访问某个页面,如果他们是:

  1. 登录
  2. 超过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:

  1. Logged in
  2. 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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晌融 2024-10-17 04:04:52

Devise 3.2.0 之前的解决方案

我想这可能就是您正在寻找的:

User.new.send(:password_digest, 'password')

它在盐为零时有效。

因此,在您的装置中,您可以执行以下操作:

one:
  email: '[email protected]'
  encrypted_password: <%= User.new.send(:password_digest, 'password') %>

Devise 版本 3.2.0 至 3.5.0 的解决方案

在 Devise 3.2.0 中,方法正是为此目的而创建的(@Inkling 更新)。对于这些版本,encrypted_pa​​ssword 应如下定义:

encrypted_password: <%= Devise.bcrypt(User, 'password') %>

其中 User 是用户模型的类。

请注意,这只适用于您使用默认加密算法 (bcrypt) 的情况。


Devise 版本 3.5.1 及更高版本的解决方案

As Katarzyna 指出:Device.bcrypt 在版本 3.5.1 中已被弃用。从该版本开始,encrypted_pa​​ssword 应如下定义:

encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>

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: '[email protected]'
  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') %>
祁梦 2024-10-17 04:04:52

我已经解决了问题 - 我错误地登录了用户。

我的测试应该阅读:

test "should get index" do
    @user = users(:one)
    sign_in @user
    get :index
    assert_response :success
    assert_select 'span#loggedin', :count => 1
 end

这也有效并消除了一行代码:

test "should get index" do
    sign_in users(:one)
    get :index
    assert_response :success
    assert_select 'span#loggedin', :count => 1
end

我对夹具的理解缺乏......

但是回到问题 - 在夹具中放入什么:

one:
 email: [email protected]
 encrypted_password: $2a$10$PoBe1MvkoGJsjMVTEjKqgeBUp.xdfzWoiDjBzQhtLAj16NqIa2fOy
 password_salt: $2a$10$PoBe1MvkoGJsjMVTEjKqge
 reset_password_token: nil
 remember_token: nil
 remember_created_at: nil
 sign_in_count: 1
 current_sign_in_at: 2011-01-02 08:31:23
 last_sign_in_at: 2011-01-02 08:31:23
 current_sign_in_ip: 127.0.0.1
 last_sign_in_ip: 127.0.0.1
 confirmation_token: nil
 confirmed_at: 2011-01-02 08:31:23
 confirmation_sent_at: 2011-01-02 08:30:59
 failed_attempts: 0
 unlock_token: nil
 locked_at: nil
 authentication_token: nil
 created_at: 2011-01-02 08:30:59
 updated_at: 2011-01-02 08:31:23
 age: 36

现在它可以工作了。如果有更简单的方法可以在开发中生成用户并将数据粘贴到固定装置中,请发布。

I have sorted out the problem - I was logging in the user incorrectly.

My test should hare read:

test "should get index" do
    @user = users(:one)
    sign_in @user
    get :index
    assert_response :success
    assert_select 'span#loggedin', :count => 1
 end

This also works and eliminates a line of code:

test "should get index" do
    sign_in users(:one)
    get :index
    assert_response :success
    assert_select 'span#loggedin', :count => 1
end

My understanding of Fixtures was lacking ...

But back to the question - what to put in fixtures:

one:
 email: [email protected]
 encrypted_password: $2a$10$PoBe1MvkoGJsjMVTEjKqgeBUp.xdfzWoiDjBzQhtLAj16NqIa2fOy
 password_salt: $2a$10$PoBe1MvkoGJsjMVTEjKqge
 reset_password_token: nil
 remember_token: nil
 remember_created_at: nil
 sign_in_count: 1
 current_sign_in_at: 2011-01-02 08:31:23
 last_sign_in_at: 2011-01-02 08:31:23
 current_sign_in_ip: 127.0.0.1
 last_sign_in_ip: 127.0.0.1
 confirmation_token: nil
 confirmed_at: 2011-01-02 08:31:23
 confirmation_sent_at: 2011-01-02 08:30:59
 failed_attempts: 0
 unlock_token: nil
 locked_at: nil
 authentication_token: nil
 created_at: 2011-01-02 08:30:59
 updated_at: 2011-01-02 08:31:23
 age: 36

Now it works. If there is an easier that generating a user in dev and pasting the data into a fixture please do post.

怎会甘心 2024-10-17 04:04:52

只是还有一个提示。这对我来说非常有用:

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.

情绪 2024-10-17 04:04:52

它对我有用。

我没有更改用户设备上的任何内容,如下所示:

one:
  id: 1
  name: MyString
  email: [email protected]
  group_id: 2
  encrypted_password: <%= Devise.bcrypt(User, 'password') %>

但我更改了 test_helper.rb 并将其添加到类 ActionController 中(这适用于所有控制器测试):

class ActionController::TestCase
    include Devise::TestHelpers

    setup do
        sign_in users(:one)
    end
end

It's working for me.

I didn't change anything on my user fixture, it's like this:

one:
  id: 1
  name: MyString
  email: [email protected]
  group_id: 2
  encrypted_password: <%= Devise.bcrypt(User, 'password') %>

But I changed my test_helper.rb and I added this to the class ActionController (this will work for all controllers tests):

class ActionController::TestCase
    include Devise::TestHelpers

    setup do
        sign_in users(:one)
    end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文