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

前端 未结 9 936
攒了一身酷
攒了一身酷 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条回答
  •  -上瘾入骨i
    2021-01-31 16:42

    RSpec 3.7 and Rails 5.x solution:

    A few of these answers were a little outdated in my case so I decided to provide an answer for those running Rails 5 and RSpec 3.7:

    it "should render js" do
      get :index, xhr: true
    
      expect(response.content_type).to eq('text/javascript')
    end
    

    Very similar to Steve's answer with a few adjustments. The first being xhr is passed as a boolean key/pair. Second is I now use expect due to should receiving deprecation warnings if used. Comparing the content_type of the response to be equal to text/javascript worked for me.

提交回复
热议问题