如果你的控制器動作看起來是這樣的:在Rails中,你如何測試Javascript響應格式的功能?
respond_to do |format|
format.html { raise 'Unsupported' }
format.js # index.js.erb
end
和你的功能測試是這樣的:
test "javascript response..." do
get :index
end
它將執行respond_to代碼塊的HTML分支。
如果你試試這個:
test "javascript response..." do
get 'index.js'
end
它執行不運行的控制器動作視圖(index.js.erb的)!
請注意,在Rails 3中,您可以使用符號,看起來整潔。 'get:index,format::js' – dazonic 2012-05-07 07:08:23
@dazonic你確定這不僅僅是由於Ruby 1.9嗎? – 2013-02-08 21:47:14
2016更新:https://github.com/rspec/rspec-rails/issues/950。它會引發InvalidCrossOriginRequest錯誤。使用@steven的答案:) – 2016-05-06 11:53:59