How to check for a JSON response using RSpec?

后端 未结 14 1303
谎友^
谎友^ 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:47

    If you want to take advantage of the hash diff Rspec provides, it is better to parse the body and compare against a hash. Simplest way I've found:

    it 'asserts json body' do
      expected_body = {
        my: 'json',
        hash: 'ok'
      }.stringify_keys
    
      expect(JSON.parse(response.body)).to eql(expected_body)
    end
    

提交回复
热议问题