[英]How to mock method call in RSpec
我需要模擬RSpec中的方法調用。 我知道如何針對常規方法調用。 但是這個方法調用是根據我在錯誤日志中得到的反饋來鏈接的。 我該如何嘲笑它?
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
這是一個很好的用例,例如使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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.