如何从rspec引用我的服务类?

时间:2017-11-14 22:43:45

标签: ruby-on-rails service rspec tdd ruby-on-rails-5

我使用Rails 5并尝试使用rspec测试服务。我创建了我的" spec"根据说明目录并将我的测试文件放在那里,这是

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

但是当我尝试运行测试时,我收到此错误抱怨无法找到我的文件......

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

从我的rspec类引用我的服务文件的正确方法是什么?

编辑:服务内容,如需要...

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

编辑:这里是应用答案

的结果

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

1 个答案:

答案 0 :(得分:5)

您不需要该文件。所以:

删除第version: "2" services: web: build: . volumes: - ./:/app ports: ["3000:3000", "3001:3001"] links: [ mongo ] mongo: image: mongo ports: [ "27017:2701" ]

移动require 'app/services/crypto_currency_service.rb'./spec/crypto_currency_service_spec.rb

然后将其放在规范文件的第一行:

./spec/services/crypto_currency_service_spec.rb.

此外,请仔细检查所有文件名,因为它们应该是:

require 'rails_helper'

编辑:

如果你使用rails 5 + rspec 3+,你应该有一个文件: ./app/services/crypto_currency_service.rb ./spec/services/crypto_currency_service_spec.rb

如果您使用的是较旧版本的rspec,那么您应该只有:yourapp/spec/rails_helper.rb,如果是后者,则在您的规范文件中执行yourapp/spec/spec_helper.rb

如果你没有任何这些文件,那么你的项目中从未初始化过rspec,请确保你的Gemfile中有'rspec-rails'gem,然后运行require 'spec_helper.rb'和{{1}在你的项目文件夹上。