rails rspec 水豚 select 不工作
rails rspec capybara select not working
在我的 rails 应用程序中,我在导航栏上有一个 select,如下所示:
<body>
<div id="wrapper">
<!-- Navigation -->
<nav role="navigation" style="margin-bottom: 0">
<div class="navbar-default sidebar hidden-sm hidden-xs" role="navigation">
<div class="sidebar-nav">
<ul class="nav" id="side-menu">
<li>
<h4 class="sidebar-title">SGPLAN</h4>
</li>
<li class="logo">
<form action="#">
<select name="" id="change_plan" class="form-control plan">
<option value="1" id="plan_id" selected="">first </option>
<option value="2" id="plan_id">other </option>
</select>
</form>
</li>
和 application.js 中的 javascript 以在用户 select 不同的选项时加载主页。
$(document).ready(function() {
//...
$('#change_plan').on('change', function(){
var str = ''
str += $( this ).val() + " ";
setCookie("plan", str,1);
window.location.href = "/";
})
});
我使用 rspec、capybara 和 capybara-webkit 为此功能编写了以下测试:
require 'rails_helper'
feature "Change plan", :js do
background do
login_as create(:admin_user), scope: :user
Agency.current = Agency.find_by(initials: 'SECTI').id
FactoryGirl.create(:other_plan)
Plan.current = Plan.find_by(name: 'first').id
end
scenario "User changes the current plan" do
visit "/milestones"
save_and_open_page
select('other', from: 'change_plan')
# within '#change_plan' do
# find("option[value='2']").click
# end
# find('#change_plan').find('option', text: 'other').select_option
expect(current_path).to eq("/")
end
end
save_and_open_page 结果如上所示 html 片段。
运行测试结果如下:
Failures:
1) Change plan User changes the current plan
Failure/Error: expect(current_path).to eq("/")
expected: "/"
got: "/milestones"
(compared using ==)
# ./spec/features/plans/change_plan_spec.rb:19:in `block (2 levels) in <top (required)>'
Finished in 1 minute 1.71 seconds (files took 2.04 seconds to load)
1 example, 1 failure
如果我在测试中使用 find('#change_plan')...
或 find("option...")
(根据注释掉的行)而不是 select,结果是一样的。
我的版本如下(来自Gemfile.lock
):
capybara (2.7.1)
capybara-webkit (1.11.1)
database_cleaner (1.5.3)
factory_girl_rails (4.7.0)
rails (4.2.5)
rspec-core (3.5.4)
rspec-expectations (3.5.0)
rspec-mocks (3.5.0)
rspec-rails (3.5.2)
和ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
我需要做什么才能让这个测试生效?我应该使用不同的测试平台吗?我们相对地致力于 rspec 但对水豚的投入较少。
更新
我终于在 Thomas 的帮助下完成了这项工作,并采纳了他提供的多项建议。
- 水豚 webkit 驱动程序出现 javascript 错误。
- 我尝试了 selenium 驱动程序,但在访问 /milestones 步骤时出现 503 错误。
- 然后我切换到 poltergeist 驱动程序,发现等待行为也是一个问题 - 所以我不得不使用 have_current_path。
您不应该将 eq
匹配器与 current_path 一起使用,因为它没有 waiting/retrying 行为。这意味着您在页面有时间更改之前检查路径。而是使用 Capybara
提供的 have_current_path 匹配器
expect(page).to have_current_path('/')
在我的 rails 应用程序中,我在导航栏上有一个 select,如下所示:
<body>
<div id="wrapper">
<!-- Navigation -->
<nav role="navigation" style="margin-bottom: 0">
<div class="navbar-default sidebar hidden-sm hidden-xs" role="navigation">
<div class="sidebar-nav">
<ul class="nav" id="side-menu">
<li>
<h4 class="sidebar-title">SGPLAN</h4>
</li>
<li class="logo">
<form action="#">
<select name="" id="change_plan" class="form-control plan">
<option value="1" id="plan_id" selected="">first </option>
<option value="2" id="plan_id">other </option>
</select>
</form>
</li>
和 application.js 中的 javascript 以在用户 select 不同的选项时加载主页。
$(document).ready(function() {
//...
$('#change_plan').on('change', function(){
var str = ''
str += $( this ).val() + " ";
setCookie("plan", str,1);
window.location.href = "/";
})
});
我使用 rspec、capybara 和 capybara-webkit 为此功能编写了以下测试:
require 'rails_helper'
feature "Change plan", :js do
background do
login_as create(:admin_user), scope: :user
Agency.current = Agency.find_by(initials: 'SECTI').id
FactoryGirl.create(:other_plan)
Plan.current = Plan.find_by(name: 'first').id
end
scenario "User changes the current plan" do
visit "/milestones"
save_and_open_page
select('other', from: 'change_plan')
# within '#change_plan' do
# find("option[value='2']").click
# end
# find('#change_plan').find('option', text: 'other').select_option
expect(current_path).to eq("/")
end
end
save_and_open_page 结果如上所示 html 片段。
运行测试结果如下:
Failures:
1) Change plan User changes the current plan
Failure/Error: expect(current_path).to eq("/")
expected: "/"
got: "/milestones"
(compared using ==)
# ./spec/features/plans/change_plan_spec.rb:19:in `block (2 levels) in <top (required)>'
Finished in 1 minute 1.71 seconds (files took 2.04 seconds to load)
1 example, 1 failure
如果我在测试中使用 find('#change_plan')...
或 find("option...")
(根据注释掉的行)而不是 select,结果是一样的。
我的版本如下(来自Gemfile.lock
):
capybara (2.7.1)
capybara-webkit (1.11.1)
database_cleaner (1.5.3)
factory_girl_rails (4.7.0)
rails (4.2.5)
rspec-core (3.5.4)
rspec-expectations (3.5.0)
rspec-mocks (3.5.0)
rspec-rails (3.5.2)
和ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
我需要做什么才能让这个测试生效?我应该使用不同的测试平台吗?我们相对地致力于 rspec 但对水豚的投入较少。
更新
我终于在 Thomas 的帮助下完成了这项工作,并采纳了他提供的多项建议。
- 水豚 webkit 驱动程序出现 javascript 错误。
- 我尝试了 selenium 驱动程序,但在访问 /milestones 步骤时出现 503 错误。
- 然后我切换到 poltergeist 驱动程序,发现等待行为也是一个问题 - 所以我不得不使用 have_current_path。
您不应该将 eq
匹配器与 current_path 一起使用,因为它没有 waiting/retrying 行为。这意味着您在页面有时间更改之前检查路径。而是使用 Capybara
expect(page).to have_current_path('/')