我构建了一个向Discorde API发送请求的客户端类.我模拟这个客户端,如下面的示例代码所示.请参考方法#mock_Client:
require 'rails_helper'
require 'discord_client'
RSpec.describe some_service_class do
describe '#call' do
let(:client) { mock_client }
it 'does this using discord_client' do
client
described_class.new.call
expect(client).to have_received(:new).once
expect(client).to have_received(:get_guild).once
end
end
private
def mock_client
client = instance_double(DiscordClient)
allow(DiscordClient).to receive(:new).and_return(client)
allow(client).to receive(:get_guild)
allow(client).to receive(:get_user)
client
end
end
然而,由于我在许多服务和rake任务中使用该客户端,我不想总是在我编写的每个规范文件中都对其进行嘲弄和删减.我可以将我的方法#MOCK_CLIENT移到哪里,这样我就可以在任何规范文件中调用它?提前谢谢!