2015-09-06 69 views
1

請幫忙寫控制器測試。如何測試一個返回json的控制器?

頁具有Ajax的按鈕約圖像顯示詳細信息:

$('#thumbsList .thumb').on('click', function(){ 
    var imageId = $(this).closest('article').attr('data-image-id'), 
     form = $(this).closest('form'); 

    $.ajax({ 
    url: '/images/' + imageId, 
    type: 'GET', 
    data: form.serialize(), 
    success: function(image){  
     $('#detailImageMeta').html(constructMeta(image)); 
    }  
    }); 
}); 

路線:

image GET /images/:id(.:format)      images#show 

imageController:

def show 
    image_detail = Image.find(image_params[:image_id]) 

    if image_detail 
     render json: { 
     user_id: image_detail.user_id 
     }, :status => 200 
    else 
     render nothing: true, :status => 404 
    end 
    end 

規格/控制器/ image_controller_spec.rb:

it 'response is success' do 
    image = FactoryGirl.create(:image)  
    get :show, id: image.id, image_id: image.id 
    expect(response).to be_success 
end 

但是當我運行在控制檯規格然後得到遵循錯誤消息:

[email protected] ~/rails/phs $ rspec spec/controllers/images_controller_spec.rb 
FFF 
Failures: 
    1) ImagesController GET #show response is success 
    Failure/Error: get :show, id: image.id, image_id: image.id 
    ActionController::ParameterMissing: 
     param is missing or the value is empty: image 

請幫忙檢查控制

+0

在你的控制器中,你有這個:'image_detail = Image.find(image_params [:image_id])',應該是:'params [:image_id]?而不是'image_params [:image_id]'?它工作嗎? –

回答

1

在你的控制器的show行動,

變化:

image_detail = Image.find(image_params[:image_id]) 

收件人:

image_detail = Image.find(params[:image_id]) 
相關問題