I'm using Rails 5 and trying to test a service using rspec. I created my "spec" directory per the instructions and placed my test file in there, which is
require 'app/services/crypto_currency_service.rb'
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
but when I try and run the test, I get this error complaining about not being able to find my file ...
localhost:myapp davea$ bundle exec rspec
An error occurred while loading ./spec/crypto_currency_service_spec.rb.
Failure/Error: require 'app/services/crypto_currency_service.rb'
LoadError:
cannot load such file -- app/services/crypto_currency_service.rb
# ./spec/crypto_currency_service_spec.rb:1:in `require'
# ./spec/crypto_currency_service_spec.rb:1:in `<top (required)>'
No examples found.
Finished in 0.00026 seconds (files took 0.07047 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
localhost:myapp davea$ ls app/services/crypto_currency_service.rb
app/services/crypto_currency_service.rb
What's the proper way to reference my service file from my rspec class?
Edit: contents of service, as requsted ...
require 'coinbase/wallet'
class CryptoCurrencyService
def sell(last_transaction)
ret = false
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}"
if sell_price > last_transaction.btc_price_in_usd * (1 + MoneyMakerThreshhold.find_buy.pct_change).to_f
payment_method = client.payment_methods()[0]
account = client.primary_account
amount_of_bitcoin = last_transaction.amount_in_usd / last_transaction.btc_price_in_usd
result = account.sell(amount: amount_of_bitcoin, currency: "BTC", payment_method: payment_method.id)
if result.status.eql?("Completed")
transaction = MoneyMakerTransacction.new({:transaction_type => "sell", :amount_in_usd => result.total.amount, :btc_price_in_usd => sell_price})
transaction.save
ret = true
end
end
ret
end
end
Edit: here's the result of applying the answer given
localhost:myapp davea$ bundle exec rspec
An error occurred while loading ./spec/services/crypto_currency_service_spec.rb.
Failure/Error: require 'rails_helper'
LoadError:
cannot load such file -- rails_helper
# ./spec/services/crypto_currency_service_spec.rb:1:in `require'
# ./spec/services/crypto_currency_service_spec.rb:1:in `<top (required)>'
No examples found.
Finished in 0.00032 seconds (files took 0.07006 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
CryptoCurrencyService
look like? – Martincrypto_currency_service.rb
file as @Martin suggested. – Starstudded