Cucmber测试按钮按下,没有成功更新页面

时间:2018-11-06 05:27:44

标签: ruby-on-rails button cucumber capybara

我正在我的Rails应用程序上进行cucmber测试。我正在尝试使用按钮登录到页面。在测试中,我将完成登录步骤,然后按按钮,然后检查文本以查看其是否与按按钮后页面上的内容匹配。我没有运气

.html代码如下

<% provide(:title, "Log in") %>
<h1>Log in</h1>

<h2>Note: authentication currently supports both a TAMU NetID or the use of CAS.</h2>
<h2>To access the CAS authentication system, click <%= link_to "here", new_member_session_path %></h2> <br />

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(:session, url: login_path) do |f| %>

      <%= f.label :netid %>
      <%= f.text_field :username, class: 'form-control' %>

      <%= f.submit "Log in", class: "btn btn-primary" %>
    <% end %>

  </div>
</div>

我的测试看起来像

    Given I am logged in as an admin user
    And I should see "admin@member.com"
    And I should see "Test Member"
    And I should see "Create Member"
    And I should see "Create Committee"

此步骤在我的webstep文件中

Given(/^I am logged in as an admin user$/) do    
visit '/sessions/new'
page.should have_content('Log in')
page.should have_content('Note: authentication currently supports both a TAMU NetID or the use of CAS.')
page.should have_content('To access the CAS authentication system, click here')
fill_in 'session_username' , :with => 'admin'
click_button 'Log in'
expect(page).to have_content 'Admin Member'

结束

但是我一直收到此错误

expected to find text "Admin Member" in "MembershipHub\nHome About Help Login\nNo member found with that NetID\nLog in\nNote: authentication currently supports both a TAMU NetID or the use of CAS.\nTo access the CAS authentication system, click here\nNetid\nAbout Help Members SEC Main Site RegistrationHub FinanceHub (Alpha)" (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/web_steps.rb:140:in `/^(?:|I )should see "([^"]*)"$/'
      features/IterationTwo_adminMember_testing.feature:6:in `Then I should see "Admin Member"'

所显示的文本实际上在登录页面上。我不确定为什么在按下登录按钮后看不到页面。

1 个答案:

答案 0 :(得分:0)

如评论中所讨论的,这里的问题是不存在正在登录的用户,因为尚未在运行测试之前设置测试所需的数据。该数据需要在测试数据库中初始化(通常使用Rails固定装置或factory_bot之类的工厂工具完成)。