在我的rails应用程序中,我在导航栏上有一个选择,如下所示:
<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加载首页。
$(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...")
(按照注释行)而不是选择,则结果是相同的。
我的版本如下(来自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,但对水豚则相对较少。
更新
我终于在托马斯的帮助下并使用了他提供的多个建议来完成这项工作。
您不应该将eq
匹配器与current_path一起使用,因为它没有等待/重试的行为。这意味着您要在页面有时间更改之前检查路径。而是使用Capybara提供的have_current_path匹配器
expect(page).to have_current_path('/')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句