RailsTutorial 章节10:如何测试只有激活的用户才能被其他人看到? [英] RailsTutorial Ch. 10: How do I test that only activated users are viewable to others?

查看:38
本文介绍了RailsTutorial 章节10:如何测试只有激活的用户才能被其他人看到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我的 Rails 应用仅在搜索结果中显示已激活的用户帐户.如果个人资料已被激活,它也只允许人们导航到个人资料页面.为了做这些事情,users_controller.rb 是这样配置的:

Currently, my Rails app only displays activated user accounts in the search results. It also only allows people to navigate to a profile page if that profile has been activated. To do these things, users_controller.rb is configured like this:

def index
  @users = User.where(activated: true).paginate(page: params[:page])
end

def show
  @user = User.find(params[:id])
  redirect_to root_url and return unless @user.activated?
end

我想知道如何使用集成测试来检查这种行为.我目前的测试是这样的:

I am wondering how to use an integration test to check this behavior. My current test is this:

test "only show profiles of activated users" do
  log_in_as(@admin)
  get users_path
  assert_template 'users/index'
  assert_select 'div.pagination'
  first_page_of_users = User.paginate(page: 1)
  first_page_of_users.each do |user|
    assert_equal true, user.activated?
  end
end

我还修改了/fixtures/users.yml 以包含尚未激活其个人资料的用户:

I have also modified /fixtures/users.yml to include a user that has not activated his profile:

non:
  name: Non Activated
  email: nonactivated@example.gov
  password_digest: <%= User.digest('password') %>
  activated: false

当我运行 rake test 时,出现此错误:

When I run rake test, I get this error:

FAIL["test_only_show_profiles_of_activated_users", UsersIndexTest, 1.271917]
test_only_show_profiles_of_activated_users#UsersIndexTest (1.27s)
    Expected: true
    Actual: false
test/integration/users_index_test.rb:40:in `block (2 levels) in <class:UsersIndexTest>'
test/integration/users_index_test.rb:39:in `block in <class:UsersIndexTest>'

谁能帮我理解为什么该测试能够检测到未激活用户的个人资料?

Can anyone help me understand why the test is able to detect profiles of non-activated users?

推荐答案

首先,让我声明,我在同一个练习中的挣扎使我来到这里,所以我有义务为您的帖子.晚了 6 个月,但也许我的回答仍能提供一些见解.

Firstly, let me state that my struggle with this same exercise landed me here, so I’m obliged for your post. 6 months late, but perhaps my response might still provide some insight.

经过大量的努力,这对我来说很清楚:

After a good deal of effort, this much is clear to me:

  1. User.paginate 对 users_controller.rb 中的实际代码来说是半冗余的,此外,它实际上导致了您的直接问题——即,在您的测试中,User.paginate 不包括 where 过滤器
  2. 直接在测试中实例化 User.paginate 并使用它来评估自己的成员是没有意义的 - 在这种情况下,测试只是测试自身而不是应用
  3. 为了实际测试应用程序,必须访问 users_controller.rb 中的 @users 实例变量,例如,my_test_users = assigns(:users)
  4. 为了彻底和测试在 users.yml 固定装置中预定义的未激活用户的存在,我们应该检查所有页面,它显示如何确定总页数的问题——will_paginate 提供了这个:my_test_users.total_pages
  5. 帮助本练习的最终见解是真正开始理解构成 REST-ful 实现的内容,即控制器-动作映射如何与命名路由和参数化一起工作,例如,get users_path, page: 1 -- 这显然调用了用户控制器,如果我们还记得 will_paginate 接受一个 :page 参数 --> User.paginate(page: params[:page]) -- 我们看到这个参数是由上面的命名路径参数提供的
  1. User.paginate is semi-redundant to the actual code in users_controller.rb, and furthermore, is actually causing your immediate problem -- namely, in your test, User.paginate doesn’t include the where filter
  2. Instantiating User.paginate directly in the test and using it to evaluate its own members doesn’t make sense - in this case, the test is only testing itself and not the app
  3. In order to actually test the app, one must access the @users instance variable in users_controller.rb, e.g., my_test_users = assigns(:users)
  4. In order to be thorough and test for presence of the non-activated user pre-defined in the users.yml fixture, we should check all pages, which presents the problem of how to determine total number of pages -- will_paginate provides this: my_test_users.total_pages
  5. The final insight that helped with this exercise was to really begin understanding what makes up a REST-ful implementation, namely, how controller-action mapping works with named routes and parameterization, e.g., get users_path, page: 1 -- this obviously calls the users controller, and if we recall that will_paginate takes a :page argument --> User.paginate(page: params[:page]) -- we see that this argument is provided by the named path parameter above

希望这些足以将各个部分放在一起以进行完整的集成测试.

So hopefully this much is enough to put the pieces together for a complete integration test.

(在尝试导航到用户配置文件时查看未激活用户是否被重定向到 root_url 的测试要简单得多)

(The test to see if non-activated user is redirected to root_url upon attempting to navigate to user profile is much more straightforward)

就其价值而言,到目前为止,此练习已被证明是本教程中最具挑战性和最有价值的练习.

For what it’s worth, so far, this exercise has proven to be the most challenging and rewarding of the tutorial.

这篇关于RailsTutorial 章节10:如何测试只有激活的用户才能被其他人看到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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