我正在尝试使用Rails 5编写测试并使用minitest这是我的测试代码......
@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)
@client.stub :sell_price, "USD" do
{"base"=>"BTC", "currency"=>"USD", "amount"=>"#{sell_price}"}
end
svc = CryptoCurrencyService.new
svc.sell(last_transaction)
但是当它在下面特写时打印出来的不是我嘲笑的售价“4000”,而是客户退回的实际售价......
def sell(last_transaction)
client = Coinbase::Wallet::Client.new(api_key: ENV['COINBASE_KEY'], api_secret: ENV['COINBASE_SECRET'])
sell_price = client.sell_price(currency: 'USD').amount
puts "buy: #{last_transaction.btc_price_in_usd} sellprice: #{sell_price} last:#{last_transaction.btc_price_in_usd}"
如何让我的模拟成功?
答案 0 :(得分:0)
Object#stub存根方法值,如文档所述
添加临时存根方法,替换块的持续时间。如果val_or_callable响应#call,则返回调用它的结果,否则返回原样值。在块的末尾清理存根。
回答你原来的问题:
client.stub :sell_price, {"base"=>"BTC", "currency"=>"USD", "amount"=>"#{sell_price}"} do
svc = CryptoCurrencyService.new
svc.sell(last_transaction)
end