我在单例类中有一些方法需要在Ruby on Rails上编写rspec。 这是示例课
class Logger
class << self
def log(event, msg)
data = Helper.send(event, msg)
PublishJob.perform(data)
end
end
end
如何编写名为Helper.send ...的测试方法并运行作业?
谢谢
答案 0 :(得分:0)
您可以通过对类的方法进行存根测试来测试Logger
是否调用Helper
和PublishJob
:
describe Logger do
let(:event) { double("Event") }
let(:msg) { double("Message") }
let(:data) { double("Data") }
before do
allow(Helper).to receive(:send).and_return(data)
allow(PublishJob).to receive(:perform)
end
before { Logger.log(event, msg) }
it "sends a message and performs a job" do
expect(Helper).to have_received(:send).with(event, msg)
expect(PublishJob).to have_received(:perform).with(data)
end
end
执行该作业的详细信息可以在该作业本身的规范文件中进行测试。