我有一個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