How to do integration testing with RSpec and Devise/CanCan?

前端 未结 6 660
情书的邮戳
情书的邮戳 2020-12-04 08:43

If I have a Devise model User, of which only those users with role :admin are allowed to view a certain url, how can I write an RSpec integration test to check that the stat

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 09:29

    I used a slightly different approach, using the Warden::Test::Helpers.

    In my spec/support/macros.rb I added:

    module RequestMacros
      include Warden::Test::Helpers
    
      # for use in request specs
      def sign_in_as_a_user
        @user ||= FactoryGirl.create :confirmed_user
        login_as @user
      end
    end
    

    And then included that in RSpec's config in spec_helper.rb:

    RSpec.configure do |config|
      config.include RequestMacros, :type => :request
    end
    

    And then in the request specs themselves:

    describe "index" do
      it "redirects to home page" do
        sign_in_as_a_user 
        visit "/url"
        page.should_not have_content 'content'
      end
    end
    

    In contrast to the post_via_redirect user_session_path method, this actually works and allows me to use current_user in before_filters, for example.

提交回复
热议问题