gpt4 book ai didi

ruby-on-rails - 如何从rspec引用我的服务类?

转载 作者:行者123 更新时间:2023-12-01 11:18:28 24 4
gpt4 key购买 nike

我正在使用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

编辑:这是应用给出的答案的结果

本地主机:myapp davea $捆绑执行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

最佳答案

您不需要该文件。所以:

删除行require 'app/services/crypto_currency_service.rb'
移动./spec/crypto_currency_service_spec.rb./spec/services/crypto_currency_service_spec.rb.
然后将其放在 spec 文件的第一行中:

require 'rails_helper'

此外,请仔细检查所有文件名,因为它们应该是:
./app/services/crypto_currency_service.rb

./spec/services/crypto_currency_service_spec.rb

编辑:

如果您使用的是Rails 5 + rspec 3+,则应该有一个文件: yourapp/spec/rails_helper.rb
如果使用的是rspec的旧版本,则应该只有: yourapp/spec/spec_helper.rb,如果是后者,请在spec文件中执行 require 'spec_helper.rb'

如果没有这些文件,则说明项目中从未初始化过rspec,请确保您的Gemfile中包含“rspec-rails” gem,然后在项目文件夹中运行 bundlerspec --init

关于ruby-on-rails - 如何从rspec引用我的服务类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47296501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com