过滤方法之前的Rspec存根

时间:2017-07-12 12:02:01

标签: ruby-on-rails unit-testing ruby-on-rails-4 rspec stubbing

我的应用程序控制器中有这样的方法

friends

我试图将其存根

before_filter :client_signed_in?

def client_signed_in?
    if cookies.signed[:client_id]
        @current_client = Client.find_by_id cookies.signed[:client_id]
        @client_signed_in = true unless @current_client.nil?
    end
end

allow(ApplicationController).to receive(:client_signed_in?).and_return(true)

但在这两种情况下都返回了allow(ApplicationController).to receive(:@client_signed_in).and_return(true)

如何解决?

2 个答案:

答案 0 :(得分:0)

尝试使用 allow(controller).to receive(:client_signed_in?).and_return(true)

或者

allow_any_instance_of(ApplicationController).to receive(:client_signed_in?).and_return(true)

答案 1 :(得分:0)

试试这个:

allow_any_instance_of(ApplicationController)
  .to receive(:client_signed_in?)
  .and_return(true)

问题在于你是Class的方法而不是对象。