I am testing my controller but requests are coming via ajax. I do not know how I can approach this problem. I am trying way like HTTP requests but I user XHR before the HTTP request. However when I run the test, I see this problem,
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)>'
I have a controller, all ajax request are recording DB in here,
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
All scenario is that A user visit the side and click the link which has some information like score and others and ajax sends the data to controller to save it as you see in controller.
But I can not solve it? How can I?
Your update method updates the @widget object without using update_attributes:
However, your test for update expects that you will call
@widget.update_attributes(params)
I think most probably, the original update method did call update_attributes and then that code was modified. That is why the test is failing now.