2010-06-20 28 views
1

應用程序堆棧:Rails3中,康康舞,設計,早該設計DoubleRenderError

我有一些嵌套資源,並希望與早該來測試他們,我得到以下DoubleRenderError:

Error: 
test: If anonymous user tries to GET index of fav_blogs should respond with 401. (BlogItemsControllerTest): 
AbstractController::DoubleRenderError: Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return". 

測試是要檢查非驗證用戶是否能夠訪問嵌套資源(他應該)

TestMethod的

context "If anonymous user tries to GET index of fav_blogs" do 
    setup do 
    get :index, :end_user_id => end_users(:end_user_one).id, :format => "xml" 
    end 
    should_respond_with 401 
    should_not_assign_to :blog_items 
end 

ControllerMethod:

def index 

    if params[:end_user_id] # if it is nested 
    authenticate_end_user! 
    authorize! :read, @end_user 

    @blog_items = @end_user.blog_items 
    else 
    @PAGE_SIZE = 10 
    page = Integer(params[:page]) || 0 

    offset = @PAGE_SIZE * page 
    @blog_items = BlogItem.limit(offset + @PAGE_SIZE).offset(offset).order('updated_at DESC').all 
    end 

    respond_with(@blog_items) 
end 

與已認證用戶做工精細所有測試 - 可能有人可以給我一個提示。非常感謝!本

回答

0

好吧,我受夠了 - 問題發生,因爲

authenticate_end_user! 
authorize! :read, @end_user 

塊。第一行是在沒有授權的情況下呈現401失敗,但無論如何,第二行將被執行。因此,將身份驗證放在before_filter中,並將授權放在另一個或您的控制器操作中。 Ben

相關問題