How to stub ApplicationController method in request spec

前端 未结 6 1468
难免孤独
难免孤独 2020-12-12 18:16

I am needing to stub the response of a current_user method in an Rspec/capybara request spec. The method is defined in ApplicationController and is

6条回答
  •  误落风尘
    2020-12-12 18:29

    For Rspec 3+ the new api is:

    For a controller test, nice and short:

    allow(controller).to receive(:current_user).and_return(@user)
    

    Or for all instances of ApplicationController:

    allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(@user)
    

提交回复
热议问题