before_filter of controller expects user to be logged in. this is why test of controller fails(work fine if i remove user authentication call from before_filter). Is it possible if user authentication call can be disabled while testing ?
class Company::BranchController < ApplicationController
before_filter **:require_user**, :only => [:show, :edit, :update, :new, :create, :index]
def create
@branch = Company::Branch.new(params[:branch])
if @branch.save
redirect_to :action => :index
else
开发者_如何学JAVA render :action => :new
end
end
end
@Antiarchitect is correct, however, given the context of your problem, I think you should consider simulating having a user signed in vs. stubbing out the method checking.
There are ways to do this with the user/login packages out there. For Devise, it provides a simple sign_in @user
method; with Authlogic, you can do UserSession.create @user
. IMHO, I think your tests will be less brittle and less tied to implementation by doing it the latter way.
May be you shouldn't disable before filter. I think you should just log in in a testing way:
before(:each) do
@current_user = Factory.create(:user)
controller.stubs(:current_user).returns(@current_user)
end
精彩评论