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