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

前端 未结 6 668
情书的邮戳
情书的邮戳 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:46

    You can create a macro (/spec/support/controller_macros.rb) and write something like :

    module ControllerMacros
      def login_user
        before(:each) do
          @request.env["devise.mapping"] = :user
          @user = Factory(:user)
          sign_in @user
        end
      end
    end
    

    You can also include any CanCan attributes you want. Then, in your spec :

    describe YourController do
        login_user
    
        it "should ..." do
    
        end
    

提交回复
热议问题