Sts
Sts

Reputation: 55

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?

Upvotes: 3

Views: 1850

Answers (2)

Nimish Gupta
Nimish Gupta

Reputation: 3175

try using allow(controller).to receive(:client_signed_in?).and_return(true)

Or

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

Upvotes: 0

nattfodd
nattfodd

Reputation: 1900

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.

Upvotes: 4

Related Questions