2009-09-28 62 views
6

我在控制器測試軌道控制,在不同的格式進行迴應

def by_xy 
     @obj = BldPoly::find_by_xy(:x => params['x'], :y => params['y']) 

     respond_to do |format| 
      format.html { render :layout => false } 
      format.xml { render :layout => false } 
      format.json { render :layout => false } 
end 

和規劃下面的函數編寫自動測試以下列方式

xml = nil 
get :by_xy, {:x => 4831, :y => 3242, :format => :json} 
assert_nothing_thrown { xml = REXML::Document.new(@response.body) } 

td = REXML::XPath.first(xml, "//result/item") 
assert_equal need_value, td.value 

,我得到 在50毫秒完成(查看:0,DB:230)| 406不可接受[http://test.host/search/by_xy/4831/3242.json]

當我錯過了在測試代碼格式 - 所有工作正常,

應該怎麼寫測試?

回答

8

我明白了這一點,實際上;這是應該如何

get :by_xy, {:x => i[:x], :y => i[:y]}, :format => :json 
+0

不知道這是否工作在一個點,但它似乎不再。 – 2015-11-17 00:29:12

+0

散列用於:x和:y是問題,請刪除散列。所以,你會有這樣的:'get:action,param1:'a',param2:'b',format :: json' – Josh 2016-03-22 14:20:54

0

爲Rails 5.1,做一個帖子的時候,我不得不包括在我的PARAMS散列

share_params = { 
    email: nil, 
    message: 'Default message.' 
    format: :json 
} 
post image_share_path(@image), params: share_params 
assert_response :unprocessable_entity 

format屬性如果沒有,我會得到裏面的錯誤ActionController::UnknownFormat我的創建控制器

def create 
    @image = Image.new(image_params) 
    if @image.save 
    flash[:success] = 'Your image was saved successfully.' 
    redirect_to @image 
    else 
    respond_to do |format| 
     format.json do 
     render json: { @image.to_json }, 
     status: :unprocessable_entity 
     end 
    end 
end