I'm using Rails 5 with Rspec 3. How do I mock a class in my Rspec method? I have the following class
require 'rails_helper'
describe CryptoCurrencyService do
describe ".sell" do
it "basic_sell" do
last_buy_price = 3000
last_transaction = MoneyMakerTransaction.new({
:transaction_type => "buy",
:amount_in_usd => "100",
:btc_price_in_usd => "#{last_buy_price}"
})
@client = Coinbase::Wallet::Client.new(api_key: ENV['COINBASE_KEY'], api_secret: ENV['COINBASE_SECRET'])
sell_price = 4000
assert sell_price > last_buy_price * (1 + MoneyMakerThreshhold.find_buy.pct_change)
allow(@client).to receive(:sell_price).and_return({"base"=>"BTC", "currency"=>"USD", "amount"=>"#{sell_price}"})
svc = CryptoCurrencyService.new
svc.sell(last_transaction)
last_transaction = MoneyMakerTransaction.find_latest_record
assert last_transaction.transaction_type, "sell"
end
end
end
Instead of actually instantiating the class "Coinbase::Wallet" in the line
@client = Coinbase::Wallet::Client.new(api_key: ENV['COINBASE_KEY'], api_secret: ENV['COINBASE_SECRET'])
I'd like to create mock taht I could then insert into my service class, which I'm testing. As it stands right now, when I run things, the actual underlying class is getting instantiated, resulting the run time error ...
1) CryptoCurrencyService.sell basic_sell
Failure/Error: payment_method = client.payment_methods()[0]
Coinbase::Wallet::AuthenticationError:
invalid api key
rspec-mocks
? github.com/rspec/rspec-mocks – Parasitic