应该匹配器 - delegate_method 不工作

Shoulda matchers - delegate_method not working

我有一个简单的委托人class

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 委托人 class

的规范
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)>'

谁能帮我弄清楚为什么这个测试失败了?谢谢

您可以使用 RSpec 模拟

显式测试此行为,而不是使用应该匹配器
it "delegates 'all' to Bank::Account" do
  expect(Bank::Account).to receive(:all)
  Service::Fs::Account.all
end

我认为断言需要一个符号。我有这个为我工作:

it { should delegate_method(:all).to(:account) }

或者使用新的 expect 语法:

it { is_expected.to delegate_method(:all).to(:account) }

见; https://thoughtbot.com/blog/shoulda-matchers-2-6-0