在Rails中,你如何测试Javascript响应格式?(In Rails, how do you functional test a Javascript response format?)

系统教程 行业动态 更新时间:2024-06-14 16:59:17
在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 end

and your functional test looks like this:

test "javascript response..." do get :index end

it will execute the HTML branch of the respond_to block.

If you try this:

test "javascript response..." do get 'index.js' end

it 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.

更多推荐

本文发布于:2023-04-16 14:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/30dbcaf3dc97facee81a862a3811a354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   测试   Javascript   Rails   format

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!