rspec模拟外部API调用

时间:2018-04-23 09:00:30

标签: ruby-on-rails rspec

我有一种方法,除其他外,调用具有自己值的外部API。

我想模仿那个电话并返回一个特定的回复

这是被调用的类/方法:

class Transaction
after_save :wallet_to_bank

[...]

def wallet_to_bank
   mangopay_payout_bankwire = MangoPay::PayOut::BankWire.create(
     'AuthorId' => receiver_wallet.provider_user_id,
     'DebitedWalletId' => receiver_wallet.provider_wallet_id,
     'DebitedFunds' => { 'Currency' => currency, 'Amount' => amount },
     'Fees' => { 'Currency' => currency, 'Amount' => 0 },
     'BankAccountId' => receiver_wallet.user.user_bank.provider_id
   )

   update_columns response: mangopay_payout_bankwire

   case mangopay_payout_bankwire['Status']
   when 'CREATED'
     update_attributes(status: :expected,
                    message: I18n.t('api.notifications.wallets_to_bank.success',
                                    transaction_id: mangopay_payout_bankwire['Id']),
                    provider_tansaction_id: mangopay_payout_bankwire['Id'])

     UserMailer.wallet_to_bank(self).deliver
   when 'FAILED'
     update_attributes(status: :failed,
                    message: mangopay_payout_bankwire['ResultCode'],
                    provider_tansaction_id: mangopay_payout_bankwire['Id'])
   end
 end

此功能

所以我想模拟对wallet_to_bank的调用,并强制响应该调用

这是我尝试过的,但它不起作用

context 'when user has done payin-payout' do
  it 'sends slack ' do
    expect(PaycarSlack.client).to receive(:ping)

   expect(MangoPay::PayOut::BankWire).to receive(:create).and_return(JSON.parse(File.read("#{Rails.root}/spec/fixtures/transaction_response_10.json")))
   transaction = create(:transaction_with_activated_users, amount: 10_000, status: :confirmed, transaction_type: :wallet_to_bank)

 end
end

任何帮助将不胜感激

0 个答案:

没有答案