如何在rails中用rspec测试respond是json格式的数据

小弟最近在给ruby项目写单元测试,用的是rspec遇到个问题想问下
代码如下:
#
# 用户管理
#

class UsersController < ApplicationController
  before_filter :monitor_required, :except=>[:destroy]
  before_filter :admin_required, :only=>[:destroy, :reset_pwd]
  before_filter :get_user, :except=>:index

  # 显示所有的管理员
  # [GET] /users
  # [FILTER] group=[:all,:admin,:configer,:monitor]
  # [ORDER]  order=column_name  
  # [PAGE]   page = n
  # [FULL URL] /users(/index).json?group=all&page=n&order=name
  def index
    group = params[:group] || "all"
    if( group == "all") 
      @users = User.find(:all, extract_common_query)
      @total = User.count
    else
      @users = User.find_all_by_group(group,  extract_common_query)
      @total = User.count(:all, :conditions=>["`group` = ?", group])
    end
    respond_to{|format|
      format.json{
        render_json_list(@users, @total)
      }
      format.xml{
        render(:xml=>@users.to_xml)
      }
    }
  end



其中extract_common_query()方法是


  # 抽取一般JS查询的三个典型参数
  # <li>order
  # <li>limit
  # <li>offset
  def extract_common_query
    params[:offset] = params[:start]
    ecq = {:limit=>25,:offset=>0}.update( params.only(:order,:limit,:offset) )
    ecq.symbolize_keys!
  end



其中render_json_list()方法是:
 
def render_json_list( data, total = data.size )
    render(:text=>"{totalCount:#{total},rows:" + data.to_json + "}", :status => 200)
end



如何用rspec测试出response 是json数据?

你可能感兴趣的:(json,单元测试,Ruby,Rails,rspec)