click_link 在 assert_select 确认 link 后无法工作
click_link not working after assert_select confirms link is there
背景
我想在 Rails 应用程序上为我的 Ruby 编写以下测试。
- 以用户身份登录。
- 访问编辑用户页面。
- 单击导航中的 link 转到“关于我们”页面。
- 实际访问“关于我们”页面并确认其模板正确。
原因是我发现了一个错误,其中 link 关于我们我将它作为 '/welcome/about/' 的参考,因此它转到 /users/welcome/about不存在。
我的代码
我的测试代码如下:
test 'welcome page from user page works' do
log_in_as(@user)
get edit_user_path(@user)
assert_select "a", { :text=> "About Us" }
click_on ("About Us")
assert_template 'welcome/about'
end
错误信息
ERROR["test_welcome_page_from_user_page_works", WelcomePageTest, 1.42307711095782]
test_welcome_page_from_user_page_works#WelcomePageTest (1.42s)
Capybara::ElementNotFound: Capybara::ElementNotFound: Unable to find link or button "About Us"
test/integration/welcome_page_test.rb:13:in `block in <class:WelcomePageTest>'
Finished in 1.42605s
1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
因为有 1 个断言,这让我认为 link 的断言过去存在,但不知何故 click_link 找不到它?
编辑 这是我测试的最终代码:
test 'welcome page from user page works' do
log_in_as(@user)
visit edit_user_path(@user)
click_on ("About Us")
assert_template 'welcome/about'
end
请注意,我还更改了硬编码的路由:
get 'about' => 'welcome#about'
对于水豚,使用 visit
方法而不是 get
visit edit_user_path(@user)
背景
我想在 Rails 应用程序上为我的 Ruby 编写以下测试。
- 以用户身份登录。
- 访问编辑用户页面。
- 单击导航中的 link 转到“关于我们”页面。
- 实际访问“关于我们”页面并确认其模板正确。
原因是我发现了一个错误,其中 link 关于我们我将它作为 '/welcome/about/' 的参考,因此它转到 /users/welcome/about不存在。
我的代码
我的测试代码如下:
test 'welcome page from user page works' do
log_in_as(@user)
get edit_user_path(@user)
assert_select "a", { :text=> "About Us" }
click_on ("About Us")
assert_template 'welcome/about'
end
错误信息
ERROR["test_welcome_page_from_user_page_works", WelcomePageTest, 1.42307711095782]
test_welcome_page_from_user_page_works#WelcomePageTest (1.42s)
Capybara::ElementNotFound: Capybara::ElementNotFound: Unable to find link or button "About Us"
test/integration/welcome_page_test.rb:13:in `block in <class:WelcomePageTest>'
Finished in 1.42605s
1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
因为有 1 个断言,这让我认为 link 的断言过去存在,但不知何故 click_link 找不到它?
编辑 这是我测试的最终代码:
test 'welcome page from user page works' do
log_in_as(@user)
visit edit_user_path(@user)
click_on ("About Us")
assert_template 'welcome/about'
end
请注意,我还更改了硬编码的路由:
get 'about' => 'welcome#about'
对于水豚,使用 visit
方法而不是 get
visit edit_user_path(@user)