RSpec控制器ajax请求测试

时间:2012-05-30 07:38:03

标签: ajax ruby-on-rails-3 rspec2

我正在测试我的控制器,但请求是通过ajax发送的。我不知道如何处理这个问题。我正在尝试像HTTP请求,但我在HTTP请求之前使用XHR。但是,当我运行测试时,我看到了这个问题,

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)>'

我有一个控制器,所有的ajax请求都在这里记录DB,

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

所有情况都是用户访问侧面并单击具有分数和其他信息的链接,ajax将数据发送到控制器以保存,如您在控制器中看到的那样。

但是我无法解决它?我怎么能?

1 个答案:

答案 0 :(得分: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        

我认为最有可能的是,原始更新方法确实调用了update_attributes,然后修改了该代码。这就是现在测试失败的原因。