2012-05-30 5 views
1

컨트롤러를 테스트하고 있지만 요청은 ajax를 통해 이루어집니다. 나는이 문제에 어떻게 접근 할 수 있는지 모른다. HTTP 요청과 같은 방식으로 시도하지만 HTTP 요청 전에 XHR 사용자입니다. 내가 테스트를 실행하지만 때, 나는 내가 컨트롤러가 RSpec 컨트롤러 ajax 요청 테스트

1) RwsController Update widget score takes widget rate information 
    Failure/Error: mock_widget.should_receive(:update_attributes).with({'these' => 'params'}) 
     (Mock "Widget_1003").update_attributes({"these"=>"params"}) 
      expected: 1 time 
      received: 0 times 
    # ./spec/controllers/rws_controller_spec.rb:28:in `block (3 levels) in <top (required)>' 

은 모든 아약스 요청이

class RwsController < ApplicationController 
    layout 'widget' 
    skip_before_filter :verify_authenticity_token, :only => [:edit, :update, :show, :getUserInfo] 
    respond_to :js, :json, :html 

    #cookie check and show exact view 
    def show 
    @widget = Widget.find_by_uuid(params[:uuid]) 
    if cookies["last_widget_id"] == @widget.uuid 
     render :action => "generate" 
    end 
    @widget = Widget.find_by_uuid(params[:uuid]) 
    end 

    def edit 
    @widget = Widget.find_by_uuid(params[:uuid]) 
    end 

    #after rating the rates and other details record on DB 
    def update 
    @widget = Widget.find_by_uuid(params[:uuid]) 
    #logger.info("score-in: " + params[:score]) 
    @widget.score = params[:score].to_i 
    @widget.total_score = params[:total_score].to_i 
    @widget.click_number = params[:click_number].to_i 
    @widget.average = params[:average].to_i 
    #set cookie 
    cookies[:last_widget_id] = {:value => @widget.uuid, :expires => 1.year.from_now} 
    @widget.save! 
    render :text => 'ok' 
    logger.info("score-out: " + @widget.score.to_s) 
    logger.info("cookie-id: " + cookies[:last_widget_id]) 
    end 

    #iframe creates and calls .js.erb file 
    def generate 
    @widget = Widget.find_by_uuid(params[:uuid]) 
    #@widget_id = @widget.id 
    respond_to do |format| 
     format.js {} 
    end 
    end 

    #recording the visitor who visit the page 
    def getUserInfo 
    data = params[:mainURL] 
    data1 = params[:mainBrowserAgent] 
    data2 = params[:mainReferer] 
    data3 = params[:mainDisplayInfo] 
    data4 = params[:currentWidgetId] 
    @widgetTraffic = WidgetTraffic.new(params[:widget_traffic]) 
    @widgetTraffic.widget_id = @widget_id 
    @widgetTraffic.main_url = data 
    @widgetTraffic.main_browser = data1 
    @widgetTraffic.main_referer = data2 
    @widgetTraffic.main_display = data3 
    @widgetTraffic.widget_id = data4 


    @widgetTraffic.save! 
    render :text => 'ok' 
    end 
end 

모든 시나리오는 사용자 측면을 방문하고 있다는 것입니다, 여기에 DB를 녹음하는이 문제를보고 점수 및 기타와 같은 정보가있는 링크를 클릭하십시오. 아약스는 컨트롤러에 표시되는대로 컨트롤러에 데이터를 저장하여 저장합니다.

하지만 해결할 수 없습니까? 내가 어떻게 할 수있는?

+0

내 대답이 문제를 해결 했습니까? – Salil

답변

1

귀하의 업데이트 방법은 사용 update_attributes없이 @widget 개체를 업데이트합니다

def update 
    @widget = Widget.find_by_uuid(params[:uuid]) 
    #logger.info("score-in: " + params[:score]) 
    @widget.score = params[:score].to_i 
    @widget.total_score = params[:total_score].to_i 
    @widget.click_number = params[:click_number].to_i 
    @widget.average = params[:average].to_i 
    #set cookie 
    cookies[:last_widget_id] = {:value => @widget.uuid, :expires => 1.year.from_now} 
    @widget.save! 

그러나, 업데이트에 대한 테스트는 widget.update_attributes (PARAMS)

1) RwsController Update widget score takes widget rate information 
    Failure/Error: mock_widget.should_receive(:update_attributes).with({'these' => 'params'}) 
     (Mock "Widget_1003").update_attributes({"these"=>"params"}) 
      expected: 1 time 
      received: 0 times   

I @ 를 호출 할 것으로 예상 아마도 원래의 업데이트 메서드가 update_attributes를 호출 한 다음 그 코드가 수정 된 것 같습니다. 그래서 지금 테스트가 실패하고 있습니다.

+0

과 같이 사양 컨트롤러를 수정했습니다. "업데이트 위젯 점수"에 대해 "위젯 요금 정보를 가져옵니다"라고 대답하십시오. WidgetTraffic.stub (: new) .with ({ 'these'=> 'params'}) {mock_widget_traffic (: 저장 => TRUE)} XHR : 풋 : 업데이트 : widget_traffic => { '다음'=> '의 PARAMS'} 양수인 (: widget_traffic) == @mock_widget_traffic response.should 끝 끝 be_success – ndrx42

+0

하지만, 나는이 시간에이 오류를 보았습니다. 1) RwsController Update 위젯 점수가 위젯 속도 정보를받습니다. 오류/오류 : put : update, id : @widget, 위젯 : Factory.attributes_for (: widget) NoMethodError : undefined meth od'score = 'for nil : NilClass # ./app/controllers/rws_controller.rb:23:in' update' # ./spec/controllers/rws_controller_spec.rb:34:in' < top (required)> ' – ndrx42

+0

즉, Widget.find_by_uuid (params [: uuid])가 nil을 반환하고 있음을 의미합니다. rails 콘솔을 사용하여 생성 된 SQL을보고 테이블 데이터를 검사하여이 문제를 디버깅 할 수 있습니다. – Salil

관련 문제