【发布时间】:2017-08-17 01:24:42
【问题描述】:
我有一个 create.js.erb 模板,其中包含以下内容:
<% if @conversation.errors.any? %>
$("#form").html("<%= j render(partial: 'form') %>");
<% else %>
Turbolinks.visit("<%= conversation_url @conversation %>");
<% end %>
我可以像这样使用assert_select_jquery 轻松测试悲伤的路径:
test 'sad path' do
# code that causes failure ommitted
# test that the error explanation div appears
assert_select_jquery :html, '#form' do
assert_select 'li', "Body can't be blank"
end
end
我在测试快乐路径时遇到问题:
test "happy path" do
post conversations_url, params: { conversation: attrs }, xhr: true
# Test that Turbolinks takes us to
# the mailbox after successful post
assert_select '.flash', "Message sent successfully."
end
测试失败,因为包含成功消息的 flash div 仅在 Turbolinks 调用之后出现。到目前为止,我能做的最好的事情是测试响应正文是否包含对 Turblinks.visit() 的调用:
expected_response = "Turbolinks.visit(\"#{conversation_url conversation}\");"
assert_match /#{expected_response}/, response.body.chomp
但这太恶心了。有没有一种 Rails 方法可以告诉控制器测试执行请求期间可能呈现的 js.erb 文件中包含的任何 Turblinks 指令?
execute_javascript_on_page
assert_select :h1, 'xyz'
【问题讨论】: