【发布时间】:2014-09-01 14:30:44
【问题描述】:
我正在编写功能规范:
步骤如下:
- 列表项
- 我添加了一条新记录
- 我将其verification_date 覆盖为nil
- 这使它带有“验证”链接的列表
- 我点击验证链接
- 如果 url 有效,屏幕应(通过 ajax)将“验证”链接更新为“2014”文本。
这在实际应用程序中当前运行良好,问题在于尝试添加一些将复制行为并反映工作过程的测试。 我似乎无法通过测试来识别来自 ajax 调用的更改文本。 我可以手动执行这些步骤,但是当我使用规范时,它看不到更新的页面。 我确保(在规范中)我只有(并且只添加了)1 条记录。
无论做什么我都会得到
1) verification lets me verify a link
Failure/Error: expect(page).to have_content(this_year)
expected to find text "2014" in "Test Linker Links New Link Gr...
规格是:
...
describe "verification", :type => :feature do
before :all do
User.create(:username => 'x@google.com', :password => 'xyz')
end
before :each do
visit '/ladmin/login'
fill_in 'username', :with => 'x@google.com'
fill_in 'password', :with => 'xyz'
find('input[value="Login"]').click
end
it "lets me verify a link" do
Link.delete_all
expect(Link.count).to eq 0
this_year=Time.now.strftime('%Y') # This is what the screen gets after verification
visit links_path
expect(page).to_not have_content(this_year) # true
l=FactoryGirl.create(:valid_url_link)
l.save
l.update_column(:verified_date, nil) # Force condition that makes 'verify' appear
expect(Link.count).to eq 1
visit links_path
find('a', text: "verify") # This seems to work. The one record has a 'verify' link
click_link("verify", match: :first) # This should change the text to 2014
sleep(7)
expect(page).to have_content(this_year)
end
end
链接工厂是:
FactoryGirl.define do
factory :link do
group {FactoryGirl.create(:group)} #:group
url_address {"http://test.com"+SecureRandom.uuid}
alt_text "examples of common situations amnd solutions"
end
factory :valid_url_link, parent: :link do
group {FactoryGirl.create(:group)} #:group
url_address {"http://www.google.com"}
alt_text "valid url"
end
...
end
js(虽然可能不是问题所在)是:
$ cat app/assets/javascripts/verifying_link.js
$(function(){
$("a[data-verifying-link]='yes'").click(function(event){
event.preventDefault();
a=$(this).parent();
a.html('<img src="assets/ajax-loader.gif">');
var id= $(this).data("id");
var row = $(this).data("tableRow");
$.ajax({
url: "/verify_link/"+id+"&table_row="+row,
type: 'GET',
success: function(r) {
$("span#verify_link_"+row).html('<span class="done">Verified</span>');
},
error: function(r) {
$("span#verify_link_"+row).html('<span class="undone">Unverified</span>');
}
});
});
});
该字段实际显示的代码有点乱(但到目前为止已经为378个链接工作了......)是:
%td.column_align
%span{id: "verify_link_#{index}"}
- if link.verified_date
- link_text = session[:full_details] == 'true' ? long_form(link.verified_date) : short_form(link.verified_date)
= render :partial => 'link_toggle', :locals => { :content => [long_form(link.verified_date), short_form(link.verified_date)], :url => verify_link_path(id: link.id, table_row: index) }
- else
- if session[:user_id]
%a{href: "#", :data => {verifying_link: 'yes', id: link.id, table_row: index}}
verify
- else
No
涉及的部分是:
- if session[:full_details] == 'true'
%span{class: "show_hide shown"}
= link_to content[0], url, title: 'Reverify this link', remote: true
%span{class: "show_hide hidden"}
= link_to content[1], url, title: 'Reverify this link', remote: true
- else
%span{class: "show_hide shown"}
= link_to content[1], url, title: 'Reverify this link', remote: true
%span{class: "show_hide hidden"}
= link_to content[0], url, title: 'Reverify this link', remote: true
这需要改进,但在实时应用中一直以这种方式工作一段时间。
为什么测试一直显示添加了链接但未经验证的页面并显示 2014?
【问题讨论】:
-
Capybara 有一个
save_and_open_page方法,可以在浏览器中打开页面的快照。在visit links_path和sleep(7)之后插入它,看看发生了什么。
标签: ruby-on-rails ruby ajax ruby-on-rails-4 rspec