Rspec功能测试混淆

时间:2016-04-25 17:51:47

标签: ruby-on-rails ruby rspec

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

TEST

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

enter image description here

查看

 <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>

更奇怪的是,当我运行&#34; save_and_open_page&#34;我期待的内容是否存在?但是,我的测试仍然无法说预期的内容没有显示WEIRD!

enter image description here

1 个答案:

答案 0 :(得分:1)

据我所知,问题是:

  1. 测试运行visit
  2. fill_in预计页面已加载
  3. 第二种期望可能是错误的,您必须使用魔法sleep 2手动等待页面加载或使用任何资本调查程序/期望自动等待

    您的测试可能如下所示:

      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