如何从 rspec 引用我的服务 class?
How do I reference my service class from rspec?
我正在使用 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 class 引用我的服务文件的正确方法是什么?
编辑:服务内容,按要求...
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
您不需要要求该文件。所以:
删除行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
,如果是后者,则在您的规范文件中执行 require 'spec_helper.rb'
。
如果您没有任何这些文件,那么 rspec 从未在您的项目中初始化,请确保您的 Gemfile 中有 'rspec-rails' gem 然后 运行 bundle
和 rspec --init
在您的项目文件夹中。
我正在使用 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 class 引用我的服务文件的正确方法是什么?
编辑:服务内容,按要求...
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
您不需要要求该文件。所以:
删除行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
,如果是后者,则在您的规范文件中执行 require 'spec_helper.rb'
。
如果您没有任何这些文件,那么 rspec 从未在您的项目中初始化,请确保您的 Gemfile 中有 'rspec-rails' gem 然后 运行 bundle
和 rspec --init
在您的项目文件夹中。