在Rails中,你如何测试Javascript响应格式?(In Rails, how do you functional test a Javascript response format?)
如果你的控制器动作如下所示:
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)!
If your controller action looks like this:
respond_to do |format| format.html { raise 'Unsupported' } format.js # index.js.erb endand your functional test looks like this:
test "javascript response..." do get :index endit will execute the HTML branch of the respond_to block.
If you try this:
test "javascript response..." do get 'index.js' endit executes the view (index.js.erb) withOUT running the controller action!
最满意答案
以正常的参数传递:format ,以此格式触发响应。
get :index, :format => 'js'无需混淆您的请求标题。
Pass in a :format with your normal params to trigger a response in that format.
get :index, :format => 'js'No need to mess with your request headers.
更多推荐
发布评论