Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec stub before filter method

I have such method in my application controller

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

i have tried to stub it

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

also

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

but in both cases it have returned nil

how to fix it?

like image 270
Sts Avatar asked May 31 '26 02:05

Sts


1 Answers

Try this one:

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

The thing is that you stub method of Class, instead of object.

like image 187
nattfodd Avatar answered Jun 02 '26 19:06

nattfodd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!