我有一个create.js.erb
模板有以下:关注Turblinks.visit()中轨控制器测试
<% 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
我m无法测试快乐路径:
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
测试失败,因为闪存di包含成功消息的v仅在Turbolinks调用后出现。我到目前为止做的最好的是测试响应正文包含Turblinks.visit()
呼叫:
expected_response = "Turbolinks.visit(\"#{conversation_url conversation}\");"
assert_match /#{expected_response}/, response.body.chomp
但这完全是毛。是否有轨道方式告诉控制器测试执行jserb文件中包含的任何Turblinks指令,这些指令可能在请求期间呈现?
execute_javascript_on_page
assert_select :h1, 'xyz'
干杯,这可能是最好的方式。我忘了水豚的一切。 – stephenmurdoch