为什么rspec告诉我我的方法不存在?

时间:2017-12-14 20:12:57

标签: ruby-on-rails methods rspec mocking ruby-on-rails-5

我正在使用Rails 5和rspec。我想为我的对象模拟一个方法“payment_methods”的调用。我已从文档中确认此方法存在(https://developers.coinbase.com/api/v2#payment-methods)。我尝试通过执行以下操作来模拟方法的返回值...

  @client = double(api_key: ENV['COINBASE_KEY'], api_secret: ENV['COINBASE_SECRET'])
  expect(Coinbase::Wallet::Client).to receive(:new).and_return(@client)

  sell_price = 4000
  assert sell_price > last_buy_price * (1 + MoneyMakerThreshhold.find_buy.pct_change)

  payment_methods = [{
    "id": "83562370-3e5c-51db-87da-752af5ab9559",
    "type": "ach_bank_account",
    "name": "International Bank *****1111",
    "currency": "USD",
    "primary_buy": true,
    "primary_sell": true,
    "allow_buy": true,
    "allow_sell": true,
    "allow_deposit": true,
    "allow_withdraw": true,
    "instant_buy": false,
    "instant_sell": false,
    "created_at": "2015-01-31T20:49:02Z",
    "updated_at": "2015-02-11T16:53:57-08:00",
    "resource": "payment_method",
    "resource_path": "/v2/payment-methods/83562370-3e5c-51db-87da-752af5ab9559"
  }]
  allow(@client).to receive(:payment_methods).and_return(payment_mehods)

不幸的是,当我运行我的测试时,我得到以下错误抱怨该方法不存在...

Failed examples:

rspec ./spec/services/crypto_currency_service_spec.rb:7 # CryptoCurrencyService.sell basic_sell

localhost:myproject davea$ bundle exec rspec
F

Failures:

  1) CryptoCurrencyService.sell basic_sell
     Failure/Error: allow(@client).to receive(:payment_methods).and_return(payment_mehods)

     NameError:
       undefined local variable or method `payment_mehods' for #<RSpec::ExampleGroups::CryptoCurrencyService::Sell:0x007ffb3d93db28>
     # ./spec/services/crypto_currency_service_spec.rb:39:in `block (3 levels) in <top (required)>'

Finished in 0.03647 seconds (files took 1.81 seconds to load)
1 example, 1 failure

我哪里错了?

0 个答案:

没有答案