首页 > 解决方案 > 当方法不存在时,如何使用 rspec 导致测试失败?

问题描述

我有一个 Rails 5 应用程序。我创建了这个工人阶级

类 PrinterWorker 包括 Sidekiq::Worker

def perform
  ...
  service = PrinterService.new(args)
  service.printtt
  ...
end

我正在使用 rspec 对我的工人进行单元测试。我有这些规格...

RSpec.describe PrinterWorker, :type => :worker do
  context "#perform" do
    let(:printer_service) { instance_double("PrinterService") }
    ...

    context "calls perform successfully" do
      before do
        ...
        allow(PrinterService).to receive(:new).and_return(printer_service)
        allow(printer_service).to receive(:printtt)
        ...

        subject.perform
      end

      it "makes proper service calls" do
        ...
        expect(printer_service).to have_received(:printtt).once
      end

我发现的问题是,如果服务“PrinterService”没有“printtt”方法,那么这个规范就可以通过了。这对我来说似乎是错误的。如果我试图调用一个不存在的方法,我觉得规范应该失败。但是,我不确定如何以不同的方式配置我的测试来实现这一点。

标签: ruby-on-railsrspecmockingruby-on-rails-5

解决方案


推荐阅读