How do I reference my service class from rspec?
Asked Answered
P

1

7

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
Parada answered 14/11, 2017 at 22:43 Comment(6)
What does the definition of CryptoCurrencyService look like?Martin
You shouldn't need to require the service, it should be loaded automatically.Starstudded
@jvillian, CryptoCurrencyService is a class I define in my "app/services/crypto_currency_service.rb" file. It starts "class CryptoCurrencyService" and has methods and fields within it.Parada
@Danielwestendorf, if I remove that line, I get an error "uninitialized constant CryptoCurrencyService" referring to the first line in my spec file, which is "describe CryptoCurrencyService do"Parada
These types of errors are usually doing to defining the class name incorrectly. Post the actual contents of your crypto_currency_service.rb file as @Martin suggested.Starstudded
Posted! See, its named the same? Maybe because its a service in the app/services directory that is causing whackiness?Parada
I
12

You shouldn't need to require that file. So:

Remove the line require 'app/services/crypto_currency_service.rb'

Move ./spec/crypto_currency_service_spec.rb to ./spec/services/crypto_currency_service_spec.rb.

Then put this in the first line of your spec file:

require 'rails_helper'

Furthermore, double check all your file names, as they should be:

./app/services/crypto_currency_service.rb

./spec/services/crypto_currency_service_spec.rb

Edit:

if you are using rails 5 + rspec 3+, you should have a file: yourapp/spec/rails_helper.rb

if you are on a older version of rspec, then you should have only: yourapp/spec/spec_helper.rb, if the later is the case, then do require 'spec_helper.rb' in your spec file.

If you don't have any of these files, then rspec was never initialized in your project, make sure you have the 'rspec-rails' gem in your Gemfile and then run bundle and rspec --init on your project folder.

Inainability answered 26/11, 2017 at 21:36 Comment(1)
K, I'm a little late to respond, but aplying your changes resulted ina n error described in an edit to my question. Basically the error was "cannot load such file -- rails_helper". Let me know what else I shoudl tryParada

© 2022 - 2024 — McMap. All rights reserved.