2016-04-25 112 views
2

我正在编写一个功能测试,现在正在尝试测试编辑操作。这对我来说有点奇怪,因为它没有看到我通常运行RSpec时所期望的结果,但是当我使用save_and_open_page命令时,它显示出我期望的实际上是在浏览器中。我认为这是一个硒问题,但我是rspec新手,我不确定。我被建议使用“睡眠”或“暂停”,但我不确定如何使用这些方法,我找不到任何好的文档。我想知道如果有人能够给我一个有效的方式来使用我当前的代码?调用RSpec睡眠

TEST

require "rails_helper" 

RSpec.feature "Edit 'Bounce Back' Message", :type => :feature do 
given!(:group) { Group.create!(name: "Group A", response: "You are now subscribed for updates") } 

scenario "Staff can see the response messages" do 
visit "/groups" 

    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") 
    expect(page).to have_content("Group a") 
end 
scenario "Staff can edit the response messages" do 
    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 have now subscribed for updates") 
end 
end 

我希望这是足够的信息,请让我知道如果你需要更多。谢谢! 我也试过使用rspec_wait,但它仍然发送错误。这里是

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

不要睡觉;让水豚等你。你的例子对我来说很好,他们在哪里失败? –

回答

1

对于这个问题,目前公认的答案是错的。无需使用rspec-wait与水豚匹配器,因为他们已经有内置的等待/重试行为。您可以调整使用Capybara.default_max_wait_time或将wait: <time in seconds>选项传递给匹配器的等待/重试的时间长度。

从阅读问题和附加图片我假设“工作人员可以看到响应消息”测试正在通过,但“工作人员可以编辑响应消息”测试失败。如果是这样,很可能是因为在编辑页面上,“您现在订阅了更新”字符串实际上是字段(文本框等)的内容而不是页面的文本内容。 have_content用于检查文本节点内容的网页上,以测试场与给定值,你会使用类似

expect(page).to have_field('Response', with: 'You have now subscribed for updates') 

,它假定在与现场以“响应”的文本相关的标签,如果不是,你可以通过字段,名称等的ID,或者从Capybara 2.7开始,你可以通过零,它只会查找具有给定值的任何字段 - 完全取决于你需要测试的内容。

expect(page).to have_field(nil, with: 'You have now subscribed for updates') 
+0

你是对的!谢谢汤姆。然而,当我尝试'expect(page).to have_field('Response','你现在已经订阅了更新')'我仍然失望。没有比赛。 – Bitwise

+0

@CameronBass正如我在我的答案中所述 - 假定有一个与文本框(或任何类型的域)相关的标签,并带有文本“响应” - 如果没有,您可以使用它的标识或名称等 - 或者如果使用Capybara 2.7+通过零,只是验证值 –

+0

@CameronBass如果你不知道如何正确调用它 - 将页面的该区域的HTML添加到您的问题 –