I need to mock a method call in RSpec. I know how to do it for a normal method call. But this method call is chained according to the feedback I'm getting in the error log. How can I mock it?
Failure/Error: response = Facebook.oauth_for_app(provider).exchange_access_token_info(token)
Koala::Facebook::OAuthTokenRequestError:
type: OAuthException, code: 190, message: Invalid OAuth access token. [HTTP 400]
module Facebook
config = Rails.application.settings
APP_ID = config[:facebook][:id]
SECRET = config[:facebook][:secret]
REWARDS_APP_ID = config[:facebook_rewards][:id]
REWARDS_SECRET = config[:facebook_rewards][:secret]
def self.config_for_app(app)
app_id = app ? "#{app.upcase}_APP_ID" : 'APP_ID'
secret = app ? "#{app.upcase}_SECRET" : 'SECRET'
[const_get(app_id), const_get(secret)]
end
def self.oauth_for_app(app)
_, app = /facebook_app_(.+)/.match(app).to_a
Koala::Facebook::OAuth.new *config_for_app(app)
end
end
before do
setup_omniauth
allow(Koala::Facebook::OAuth).to receive(:refresh_facebook_token).and_return(true)
end
This is a good use case for instance doubles IMO,
oauth = instance_double(Koala::Facebook::OAuth)
allow(Koala::Facebook::OAuth).to receive(:new).with(config).and_return(oauth)
allow(oauth).to receive(:some_other_method)
我能够使用receive_message_chain
找到另一个解决方案
allow(Facebook).to receive_message_chain(:oauth_for_app, :exchange_access_token_info).and_return('access_token' => '123', 'expires' => 500_000)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.