Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference my service class from rspec?

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
like image 440
Dave Avatar asked Nov 14 '17 22:11

Dave


People also ask

How do I write my first RSpec test?

This is the initial code for writing your first RSpec test. You need to require the rspec gem. Then you need to create a describe block to group all your tests together & to tell RSpec which class you are testing. Next is the it block: This is the test name, plus a way to group together all the components of the test itself.

What does describe mean in RSpec?

The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.

What does the filename of a RSpec test file do?

The convention of RSpec is that all test files must end with “_spec” in their filename. Below is the explanation of what each line does in the test file. This line will load the necessary test configuration from spec/rails_helper.rb, which is needed for the test to run.

How do you pass an argument to describe in RSpec?

You also need to pass a block argument to describe, this will contain the individual tests, or as they are known in RSpec, the “Examples”. The block is just a Ruby block designated by the Ruby do/end keywords. The context keyword is similar to describe. It too can accept a class name and/or string argument.


1 Answers

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.

like image 181
fabriciofreitag Avatar answered Sep 24 '22 21:09

fabriciofreitag