spec: call external methods once

def index @item = Item.find(params[:item]) unless @item.users.include?(current_user) redirect_to user_path(current_user) return end /typo:code

with a spec that includes before(:each) do @user = mock_model(User) login @user end /typo:code

and a spec helper of

def login(user) controller.should_receive(:current_user).and_return(user) end /typo:code gives a double-receipt error for the method current_user. the value of the method call current_user is static. we use this to justify a local variable to hold the value. this reduces the number of method calls and protects against certain errors when the current_user might change accidentally half way through the method.

the method becomes def index user = current_user @item = Item.find(params[:item]) unless @item.users.include?(user) redirect_to user_path(user) return end /typo:code