我可能会这样做错了.我先做规格,BDD / TDD,碰撞.
我有这个application_controller_spec.rb
require "spec_helper" describe ApplicationController do describe "current_user" do it "should return nil if no one is logged in" do subject.current_user.should be_nil end it "should return currently logged in user" do hash = {user_id: "my_id"} subject.should_receive(:session).and_return hash subject.current_user.should == "my_id" end end end
没有受保护的关键字,它工作得很好.
application_controller.rb
class ApplicationController < ActionController::Base protect_from_forgery helper_method :current_user protected def current_user session[:user_id] end end
与受保护启用,我得到这个错误msg
NoMethodError: protected method `current_user' called for #<ApplicationController:0x2a90888>
我应该能够使用helper_method来测试…任何建议?