【问题标题】:Rspec feature test confusionRspec 功能测试混淆
【发布时间】:2016-04-26 14:38:25
【问题描述】:

我有一个试图验证编辑操作的功能测试。这是一个超级简单的测试,但我写的并不多,而且我犯了一些我无法弄清楚的错误。基本上,当访问页面时,它不会呈现要编辑的文本信息,并且“更新”按钮不存在。为了清楚起见,这是我的错误和代码。

测试

scenario "Staff can edit the response messages" do
  group = Group.create!(name: "Group A", response: "You are now subscribed for updates")
  visit "/groups/#{group.id}/edit"

  user = FactoryGirl.create(:user)
  fill_in "Email", with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"

  expect(page).to have_content("You are now subscribed for updates")
end

查看

 <div class="container text-center">
  <div class="row">
  <div class="col-lg-8 col-lg-offset-2 well">
     <%= form_for @group do |form| %>
       <div class="form-group">
         <%= form.label :body, "Edit The Response:", class: "message_label"%>
         <%= form.text_field :response, class: "form-control", placeholder: "New things are happening!" %>
       </div>
       <%= form.submit "Update", class: "btn btn-primary" %>
    <% end %>
  </div>
 </div>
</div>

更奇怪的是,当我运行“save_and_open_page”时,我期望的内容是存在的?但是,我的测试仍然失败,说没有显示预期的内容很奇怪!

【问题讨论】:

  • 您好。在您看来,我看不到您再次测试的文本应该显示在哪里。带有失败消息的图像没有显示页面上实际存在的所有文本(在右侧被截断) 一般来说,我建议使用 save_and_open_page 或 save_and_open_screenshot (如果你的包中有 capybara-screenshot)更好地了解页面实际显示的内容。
  • 是的,这更奇怪,因为如果我使用 save_and_open_page 它会在页面上显示我期望的内容,但如果我运行测试它不会显示?
  • 在唱歌后插入pause 3,仅用于测试目的
  • 唱完后插入停顿 3?很抱歉,但我不确定这意味着什么?
  • @Cambass sleep 3

标签: ruby-on-rails ruby rspec


【解决方案1】:

据我所知,问题是:

  1. 测试运行visit
  2. fill_in 预计页面已加载

第二个期望可能是错误的,您必须等待页面加载,要么使用魔法 sleep 2 手动加载,要么使用任何水豚的 finder/expectation 自动加载

您的测试可能如下所示:

  group = Group.create!(name: "Group A", response: "You are now subscribed for updates")
  user = FactoryGirl.create(:user)

  visit "/groups/#{group.id}/edit"
  // <= check any static text to ensure the page is loaded
  // expect will wait for a defailt timeout ~ 2 seconds
  expect(page).to have_content("Tulim time text")

  fill_in "Email", with: user.email

  // ...

手动延迟示例

  visit "/groups/#{group.id}/edit"
  // wait a few seconds for page loading
  sleep 5
  fill_in "Email", with: user.email

【讨论】:

  • 如何添加睡眠2?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多