【问题标题】:testing with minitest capybara rails link not found未找到使用 minitest capybara rails 链接进行测试
【发布时间】:2013-12-05 19:16:40
【问题描述】:

我目前正在使用 minitest capybara 进行测试,但我的一项测试出现错误

Unable to find link or button "Edit Profile"

这是我的测试

require "test_helper"

feature "as a student I want a working user system so grade = good" do
  scenario "users can update profile" do
    dude_sign_up
    dude_log_in
    click_on "Edit Profile"
    click_on "Update"
    page.must_have_content "Profile was successfully updated"
  end
end

测试助手的两个助手方法

def dude_sign_up
  visit new_user_path
  fill_in "Name", with: 'thedude'
  fill_in "Email", with: "dude@dudecool.com"
  fill_in "Password", with: 'password'
  fill_in "Password confirmation", with: 'password'
  click_on "Sign Up"
end

def dude_log_in
  visit posts_path
  fill_in "Email", with: "dude@dudecool.com"
  fill_in "Password", with: 'password'
  click_on "Log In"
end

如果需要的话,这里是我在 application.html.erb 中渲染的 _nav,介于收益之间和之上

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  <div class="container">
    <ul class="nav navbar-nav navbar-right">
      <li><%= link_to 'Home', root_path %></li>
      <li><%= link_to 'About', about_path %></li>
      <li><%= link_to "The Blog", posts_path %></li>
      <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Registration<b class="caret"></b></a>
        <ul class="dropdown-menu">
          <% if current_user %>
          <td><%= link_to 'Edit Profile', edit_user_path(current_user) %></td>
          <li><%= link_to "Users", users_path %></li>
          <li><%= link_to "Log out", logout_path %></li>
          <% else %>
          <li><%= link_to 'Sign Up', signup_path %></a></li>
          <li><%= link_to 'Log In', login_path %></li>
          <% end %>
        </ul>
      </li>
    </ul>
  </div>
</nav>

如果我点击所有内容并浏览它,它就可以正常工作。我没有使用固定装置是我的问题的原因吗?

【问题讨论】:

  • 您确定用户登录成功了吗?
  • 是的,因为我运行了另一个测试以查看登录的 flash 消息是否出现并且确实出现了,我的意思是测试通过了

标签: ruby-on-rails capybara minitest


【解决方案1】:

当 Capybara 查找元素时,它只会考虑对用户可见的元素。

鉴于您页面中的命名,我猜测用户首次登录时“编辑个人资料”是不可见的。要查看该链接,用户可能需要先单击“注册”链接。

Capybara 需要执行相同的工作流程。尝试添加click_on "Registration"

scenario "users can update profile" do
  dude_sign_up
  dude_log_in
  click_on "Registration"
  click_on "Edit Profile"
  click_on "Update"
  page.must_have_content "Profile was successfully updated"
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多