How to check for a JSON response using RSpec?

后端 未结 14 1287
谎友^
谎友^ 2020-11-29 16:03

I have the following code in my controller:

format.json { render :json => { 
        :flashcard  => @flashcard,
        :lesson     => @lesson,
             


        
14条回答
  •  天命终不由人
    2020-11-29 16:53

    Another approach to test just for a JSON response (not that the content within contains an expected value), is to parse the response using ActiveSupport:

    ActiveSupport::JSON.decode(response.body).should_not be_nil
    

    If the response is not parsable JSON an exception will be thrown and the test will fail.

提交回复
热议问题