In Rails, how do you functional test a Javascript response format?

前端 未结 9 935
攒了一身酷
攒了一身酷 2021-01-31 15:41

If your controller action looks like this:

respond_to do |format|
  format.html { raise \'Unsupported\' }
  format.js # index.js.erb
end

and yo

9条回答
  •  被撕碎了的回忆
    2021-01-31 16:29

    Many of the above answers are outdated.

    The correct way to do it in RSpec 3+ is post some_path, xhr: true.

    Deprecation warning straight from RSpec itself, when attempting to use xhr :post, "some_path":

    DEPRECATION WARNING: `xhr` and `xml_http_request` are deprecated and will be removed in Rails 5.1.
    Switch to e.g. `post comments_path, params: { comment: { body: 'Honey bunny' } }, xhr: true`.
    

    Also, xhr :post, "some_path" results in some funky errors that doesn't happen with post some_path, xhr: true.

提交回复
热议问题