簡體   English   中英

如何為控制器編寫適當的rspec測試

[英]How to write proper rspec test for controller

我有一個名為“應用程序控制器”的控制器,下面是我要為其編寫測試的下一個方法:

  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

如何為此編寫適當的測試,應該是哪種測試?

您不應該測試實現。 嘗試測試“此控制器操作對非管理員用戶應如何表現”,它可能看起來像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM