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

前端 未结 9 932
攒了一身酷
攒了一身酷 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:22

    I had similar problem:

    # controller
    def create
      respond_to do |format|
        format.js
      end
    end
    
    # test
    
    test "call format js" do
      record = promos(:one)
      post some_url(record)
      assert true
    end
    

    and the result was this:

    > rails test
    Error:
    ActionController::UnknownFormat: ActionController::UnknownFormat
    

    I fixed it with this adjusting to the test(adding headers):

    test "call format js" do
      record = promos(:one)
      headers = { "accept" => "text/javascript" }
      post some_url(record), headers: headers
      assert true
    end
    

    rails (6.0.0.beta3)

提交回复
热议问题