应该匹配 - delegate_method不工作

问题描述 投票:0回答:1

我有一个简单的委托者类

class Service::Fs::Account < DelegateClass(Bank::Account)
  extend SingleForwardable

  def initialize(args={})
    @account = Bank::Account.new(args)
    super(@account)
  end

  def_delegators :"Bank::Account", :all, :create, :update
end

从我的rails控制台,一切正常

2.1.8 :002 > Service::Fs::Account.all
  Bank::Account Load (1.2ms)  SELECT "bank_accounts".* FROM "bank_accounts"
 => #<ActiveRecord::Relation []> 

这是我对Account委托人类的规范

require 'spec_helper'

describe Service::Fs::Account do
  describe 'delegations' do
    it { should delegate_method(:all).to(Bank::Account) }
  end
end

测试失败,出现以下错误

 Failure/Error: it { should delegate_method(:all).to(Bank::Account) }
   Expected Service::Fs::Account to delegate #all to #Bank::Account object
   Method calls sent to Service::Fs::Account#Bank::Account: (none)
 # ./spec/models/service/fs/account_spec.rb:5:in `block (3 levels) in <top (required)>'

任何人都可以帮我弄清楚为什么这个测试失败了?谢谢

ruby-on-rails ruby rspec shoulda
1个回答
2
投票

您可以使用RSpec模拟显式测试此行为,而不是使用should matchers

it "delegates 'all' to Bank::Account" do
  expect(Bank::Account).to receive(:all)
  Service::Fs::Account.all
end
© www.soinside.com 2019 - 2024. All rights reserved.