Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails集成测试语法-使用get:edit vs get edit\u user\u path_Ruby On Rails_Syntax_Get_Integration Testing_Patch - Fatal编程技术网

Ruby on rails Rails集成测试语法-使用get:edit vs get edit\u user\u path

Ruby on rails Rails集成测试语法-使用get:edit vs get edit\u user\u path,ruby-on-rails,syntax,get,integration-testing,patch,Ruby On Rails,Syntax,Get,Integration Testing,Patch,我正在阅读Hartl在中的练习,并进入了关于为要求登录用户编写集成测试的部分 我注意到清单9.17(I)使用了 而清单9.14(ii)使用: 有什么区别?为了测试登录的用户在未登录时是否被重定向到主页,后者可以工作,而前者会抛出一个错误 从概念上讲,上面的两个语句看起来是相同的,i)调用控制器操作,而ii)路由到资源 是吗?事实上,这两个代码在概念上是等价的 他们通过userscocontroller中的edit操作,向users/edit.html.erb发出GET请求,也在做同样的事情 但是

我正在阅读Hartl在中的练习,并进入了关于为要求登录用户编写集成测试的部分

我注意到清单9.17(I)使用了

而清单9.14(ii)使用:

有什么区别?为了测试登录的用户在未登录时是否被重定向到主页,后者可以工作,而前者会抛出一个错误

从概念上讲,上面的两个语句看起来是相同的,i)调用控制器操作,而ii)路由到资源


是吗?

事实上,这两个代码在概念上是等价的

他们通过
userscocontroller
中的
edit
操作,向
users/edit.html.erb
发出
GET
请求,也在做同样的事情

但是,您测试上述行为的环境是不同的


代码

get :edit, id: @user
在测试控制器的上下文中(
userscoontroller
)。因此,您只需要指定操作(在本例中为“编辑”)以及必要的参数(在本例中为“id”)

是在模拟用户浏览你的应用程序的上下文中,你可以通过集成测试来完成。在这里,您需要精确了解路由路径,因为可能涉及不同的控制器。也就是说,调用操作的上下文不限于一个特定的控制器。 例如,在中,我们有一个模拟用户注销的集成测试:

  test "login with valid information followed by logout" do
    get login_path
    post login_path, session: { email: @user.email, password: 'password' }
    assert is_logged_in?
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    # Simulate a user clicking logout in a second window.
    delete logout_path
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
  end
在这个集成测试中,涉及到两个控制器:
userscoontroller
sessioncontroller

具体而言:

  • user\u路径(@user)
    assert\u中选择“a[href=?]”,
    用户路径(@user)
    是关于
    userscocontroller
    中的
    show
    操作
  • get login\u path
    中的
    login\u path
    调用中的
    new
    操作
    会话控制器

  • 登录路径
    登录后路径中,会话:{email:@user.email,
    密码:“password”}
    调用中的
    创建
    操作
    会话控制器

  • login\u path
    assert\u选择“a[href=?]”中,login\u path
    是关于
    sessioncontroller
    中的
    new
    操作
  • 注销路径
    断言中选择“a[href=?]”,注销路径
    为 关于
    sessioncontroller
    中的
    destroy
    操作
这在集成测试中是可能的,因为我们在调用某些操作时不局限于特定的控制器

这也解释了为什么您提供的第一个代码

get :edit, id: @user
在集成测试中引发错误。 它没有指定在哪个控制器中调用
edit
操作。(例如,它可以是
accountactivationcontroller
passwordresetscocontroller
中的
edit
操作)

还有,你提到的那一点

i) 调用控制器操作,而ii)路由到资源

这很重要

控制器测试纯粹是通过发出“低级”或“机器级”命令来测试控制器。也就是说,我们直接向控制器操作(
edit
在您的情况下)提交特定的HTTP请求(
PATCH
在您的情况下),而不必担心模拟用户浏览您的应用程序(即,用户实际单击链接“edit Profile”),您可以通过更多的“高级”集成测试来完成

get edit_user_path(@user)
  test "login with valid information followed by logout" do
    get login_path
    post login_path, session: { email: @user.email, password: 'password' }
    assert is_logged_in?
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    # Simulate a user clicking logout in a second window.
    delete logout_path
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
  end
get :edit, id: @user