我有一个简单的委托者类
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)>'
任何人都可以帮我弄清楚为什么这个测试失败了?谢谢
您可以使用RSpec模拟显式测试此行为,而不是使用should matchers
it "delegates 'all' to Bank::Account" do
expect(Bank::Account).to receive(:all)
Service::Fs::Account.all
end