Link 一段时间后水豚不再隐藏
Link doesn't hide after some time capybara
我使用水豚来测试位于我的 Comment 模型中的这段代码(5 分钟是常规的):
def editable?
self.created_at < (Time.now - 5.minute)
end
link的观点:
- unless comment.editable?
= link_to 'Edit', edit_category_theme_comment_path(@category, @theme, comment)
所以 5 分钟后 link 编辑必须从页面隐藏(但我们需要刷新页面)。这是我在 RSpec 中用于创建评论和测试 link-隐藏功能的代码:
def create_comment(options={})
options[:content] ||= 'I am a comment'
visit category_theme_path(category, theme)
within '.comment-form' do
fill_in 'Content', with: options[:content]
click_button 'Submit'
end
end
context 'Comment has content' do
before(:each) { create_comment }
it 'hides the edit symbol due 5 minutes after comment was created' do
using_wait_time 400 do
visit category_theme_path(category, theme)
save_and_open_page
expect(page).to have_no_css('.comment-edit')
end
end
end
但是我得到了:Failure/Error: expect(page).to have_no_css('.comment-edit')expected #has_no_css?(".comment-edit") to return true, got false
我也尝试使用page.reload!
、expect(page).to have_no_css('.comment-edit', wait: 400)
等相关人员,但是水豚不想等。也许我将 using_wait_time
用于错误的地方,如果那样 - 我该如何测试?
使用 5 分钟前的种子数据并访问该页面。像这样不用等五分钟
还有你的代码
- unless comment.editable?
= link_to 'Edit', edit_category_theme_comment_path(@category, @theme, comment)
应该可以阅读
- if comment.editable?
= link_to 'Edit', edit_category_theme_comment_path(@category, @theme, comment)
您的测试方法存在很多问题。您尝试失败的原因是因为 using_wait_time 只是设置了 Capybaras 匹配器等待他们的期望变为现实的时间量,它实际上并没有让程序等待。因此 expect(page).to have_no_css('.comment-edit')
将等待您的 using_wait_time
指定的时间,每 50 毫秒左右重新检查一次页面的内容,但它从不重新加载页面并且不会在加载页面之前等待。对于您的工作方式,您需要在访问页面之前先睡觉
it 'hides the edit symbol due 5 minutes after comment was created' do
sleep 5.minutes
visit category_theme_path(category, theme)
save_and_open_page
expect(page).to have_no_css('.comment-edit')
end
然而,这是一个糟糕的想法,因为您的测试会变得非常慢。
您可以使用 FactoryGirl 之类的东西并指定 5 分钟前的 created_at,而不是那种方法,您已经可以生成您的测试评论 "old"(正如 pascal betz 所建议的那样)。
FactoryGirl.create(:comment, created_at: 5.minutes.ago)
或者,如果您想继续通过界面创建评论,请添加类似 Timecop gem 的内容,您可以执行
Timecop.travel(5.minutes.from_now) do
visit category_theme_path(category, theme)
save_and_open_page
expect(page).to have_no_css('.comment-edit')
end
这将在访问页面之前将时钟向前移动 5 分钟,然后在块结束后将其重置为正常。
此外,您的 editable?
方法比较方向错误,应该是
def editable?
self.created_at > (Time.now - 5.minute)
end
那么视图应该是
- if comment.editable?
= link_to 'Edit', edit_category_theme_comment_path(@category, @theme, comment)
我使用水豚来测试位于我的 Comment 模型中的这段代码(5 分钟是常规的):
def editable?
self.created_at < (Time.now - 5.minute)
end
link的观点:
- unless comment.editable?
= link_to 'Edit', edit_category_theme_comment_path(@category, @theme, comment)
所以 5 分钟后 link 编辑必须从页面隐藏(但我们需要刷新页面)。这是我在 RSpec 中用于创建评论和测试 link-隐藏功能的代码:
def create_comment(options={})
options[:content] ||= 'I am a comment'
visit category_theme_path(category, theme)
within '.comment-form' do
fill_in 'Content', with: options[:content]
click_button 'Submit'
end
end
context 'Comment has content' do
before(:each) { create_comment }
it 'hides the edit symbol due 5 minutes after comment was created' do
using_wait_time 400 do
visit category_theme_path(category, theme)
save_and_open_page
expect(page).to have_no_css('.comment-edit')
end
end
end
但是我得到了:Failure/Error: expect(page).to have_no_css('.comment-edit')expected #has_no_css?(".comment-edit") to return true, got false
我也尝试使用page.reload!
、expect(page).to have_no_css('.comment-edit', wait: 400)
等相关人员,但是水豚不想等。也许我将 using_wait_time
用于错误的地方,如果那样 - 我该如何测试?
使用 5 分钟前的种子数据并访问该页面。像这样不用等五分钟
还有你的代码
- unless comment.editable?
= link_to 'Edit', edit_category_theme_comment_path(@category, @theme, comment)
应该可以阅读
- if comment.editable?
= link_to 'Edit', edit_category_theme_comment_path(@category, @theme, comment)
您的测试方法存在很多问题。您尝试失败的原因是因为 using_wait_time 只是设置了 Capybaras 匹配器等待他们的期望变为现实的时间量,它实际上并没有让程序等待。因此 expect(page).to have_no_css('.comment-edit')
将等待您的 using_wait_time
指定的时间,每 50 毫秒左右重新检查一次页面的内容,但它从不重新加载页面并且不会在加载页面之前等待。对于您的工作方式,您需要在访问页面之前先睡觉
it 'hides the edit symbol due 5 minutes after comment was created' do
sleep 5.minutes
visit category_theme_path(category, theme)
save_and_open_page
expect(page).to have_no_css('.comment-edit')
end
然而,这是一个糟糕的想法,因为您的测试会变得非常慢。
您可以使用 FactoryGirl 之类的东西并指定 5 分钟前的 created_at,而不是那种方法,您已经可以生成您的测试评论 "old"(正如 pascal betz 所建议的那样)。
FactoryGirl.create(:comment, created_at: 5.minutes.ago)
或者,如果您想继续通过界面创建评论,请添加类似 Timecop gem 的内容,您可以执行
Timecop.travel(5.minutes.from_now) do
visit category_theme_path(category, theme)
save_and_open_page
expect(page).to have_no_css('.comment-edit')
end
这将在访问页面之前将时钟向前移动 5 分钟,然后在块结束后将其重置为正常。
此外,您的 editable?
方法比较方向错误,应该是
def editable?
self.created_at > (Time.now - 5.minute)
end
那么视图应该是
- if comment.editable?
= link_to 'Edit', edit_category_theme_comment_path(@category, @theme, comment)