On May 30, 2012, at 3:18 AM, zombiebit wrote:
> Hey,
> I want to test my Rails app. When you click the image ajax sends to data to
> DB and the data is updated in there. When I try to test, I faced with 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)>''
>
>
> *This is my actual controller which I try to test,*
>
>
> 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!
This is not #update_attributes
> #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
>
>
> *And this is my test controller,
> *
> require ''spec_helper''
>
> describe RwsController do
>
> def mock_widget(stubs={})
> @mock_widget ||= mock_model(Widget, stubs).as_null_object
> end
>
> describe "Get widget" do
> it "gets generated widget in iframe" do
> Widget.stub(:find_by_uuid).with("10") { mock_widget(:save
=> true) }
> get :generate ,:uuid => "10"
> assigns(:widget) == @widget
> response.should render_template(:js => "generate")
> end
>
> it "can not get generated widget in iframe" do
> Widget.stub(:find_by_uuid).with("10") { mock_widget(:save
=> false) }
> get :generate, :uuid => "10"
> assigns(:widget) == @widget
> response.code.should == "406"
> end
> end
>
> describe "Update widget score" do
> it "takes widget rate information" do
>
>
Widget.stub(:find_by_uuid).with("6").and_return(mock_widget(:update_attributes
> => true))
Need to pass #save! as the stub....
>
mock_widget.should_receive(:update_attributes).with({''these''
=>
> ''params''})
> xhr :put, :update, :uuid => "6", :widget =>
{''these'' => ''params''},
> :format => :js
> response.should be_success
> end
> end
>
> end
>
> *Where is the stuck point? How should I solve it?*
>
> --
> View this message in context:
http://ruby.11.n6.nabble.com/RSpec-testing-update-action-with-ajax-tp4975810.html
> Sent from the rspec-users mailing list archive at Nabble.com.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users