2015-08-16 106 views
0

我控制器調用一個方法應用控制器,我想要寫測試正確的RSpec測試:如何編寫控制器

def require_admin 
    p current_user.role 
    return if current_user && current_user.role == 'admin' 
    flash[:error] = "You are not an admin" 
    redirect_to root_path 
    end 

如何編寫正確的測試它,和什麼樣的測試應該它是?

+0

expect(response).to redirect_to() – user3624268

回答

0

您不應該測試實現。嘗試測試'這個控制器動作應該如何處理非管理員用戶',它可能看起來像這樣:

context 'user who is not an admin' do 
    before do 
    user.role = 'not_admin' 
    get :some_action 
    end 

    it 'is redirected to root_path' do   
    expect(response).to redirect_to('/') 
    end 
    ... 
end