1

我正在测试我的控制器,但请求是通过 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 请求都在这里记录数据库,

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 将数据发送到控制器以将其保存,如您在控制器中看到的那样。

但我不能解决吗?我怎样才能?

4

1 回答 1

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,然后修改了该代码。这就是为什么现在测试失败了。

于 2012-05-31T07:36:50.700 回答