Testing filters ApplicationController, Rails

I am trying to use rspec to check the filter that I have in my ApplicationController.

In spec/controllers/application_controller_spec.rbI have:

require 'spec_helper'
describe ApplicationController do
  it 'removes the flash after xhr requests' do      
      controller.stub!(:ajaxaction).and_return(flash[:notice]='FLASHNOTICE')
      controller.stub!(:regularaction).and_return()
      xhr :get, :ajaxaction
      flash[:notice].should == 'FLASHNOTICE'
      get :regularaction
      flash[:notice].should be_nil
  end
end

My intention was for the test to make fun of the ajax action that installs the flash, and then check the next request that the flash was cleared.

I get a routing error:

 Failure/Error: xhr :get, :ajaxaction
 ActionController::RoutingError:
   No route matches {:controller=>"application", :action=>"ajaxaction"}

However, I expect several errors to not match the way I try to verify this.

For a link, the filter is called in ApplicationControllerlike:

  after_filter :no_xhr_flashes

  def no_xhr_flashes
    flash.discard if request.xhr?
  end

How can I create mock methods ApplicationControllerfor filter testing a wide range of applications?

+5
source share
1

RSpec, RSEE .

application_controller_spec.rb, .

.

require 'spec_helper'

describe ApplicationController do
  describe "#no_xhr_flashes" do
    controller do
      after_filter :no_xhr_flashes

      def ajaxaction
        render :nothing => true
      end
    end

    it 'removes the flash after xhr requests' do      
      controller.stub!(:ajaxaction).and_return(flash[:notice]='FLASHNOTICE')
      controller.stub!(:regularaction).and_return()
      xhr :get, :ajaxaction
      flash[:notice].should == 'FLASHNOTICE'
      get :regularaction
      flash[:notice].should be_nil
    end
  end
end
+8

All Articles