首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用元编程为多个工人编写单个规范

使用元编程为多个工人编写单个规范
EN

Stack Overflow用户
提问于 2017-07-14 07:19:59
回答 1查看 323关注 0票数 1

我写了一份侧写工人说明书。所以现在我们有四名工人,他们的规格几乎相同。所有规范都将对some_method进行测试,并检查任务是否等同。

我的示例员工代码:

代码语言:javascript
复制
RSpec.describe HardWorker do
  subject(:worker) { described_class.new }

  describe "perform" do
    let(:some_id) { instance_double("String") }

    it "calls Hard working operation" do
      expect(HardWorkingOperation).to receive(:one_method)
        .with(some_id: some_id)

      worker.perform(some_id)
    end

    it "enqueues a HardWork worker" do
      HardWorker.perform_async(some_id)
      expect(HardWorker.jobs.size).to eq 1
    end
  end
end

第二个样本规范:

代码语言:javascript
复制
RSpec.describe AnotherWorker do
  subject(:worker) { described_class.new }

  describe "perform" do
    let(:key1){double("Integer")}
    let(:key2){double("String")}
    let(:options) do 
      {
        :key1 => key1, 
        :key2_ref => key2
      }
    end

    it "calls method_data" do
      expect(AnotherOperation).to receive(:another_method)
        .with(options["key1"], options["key2"])

      worker.perform(options)
    end

    it "enqueues a Another worker" do
        AnotherWorker.perform_async(options)
        expect(AnotherWorker.jobs.size).to eq 1
    end
  end
end

我想要编写一个单独的规范,测试接收某种方法的所有工作人员(对于不同的方法可以是不同的),并且作业已经排队。我怎么才能做得最好呢?有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-14 07:38:20

您可以使用共享示例。假设它们都有一个将执行call的“操作”类,可能如下所示:

代码语言:javascript
复制
shared_examples_for "a sidekiq worker" do |operation_klass|
  subject(:worker) { described_class.new }

  describe "perform" do
    let(:some_id) { instance_double("String") }

    it "calls some operation" do
      expect(operation_klass).to receive(:call).with(some_id: some_id)
      worker.perform(some_id)
    end

    it "enqueues a worker" do
      described_class.perform_async(some_id)
      expect(described_class.jobs.size).to eq 1
    end
  end
end

RSpec.describe HardWorker do
  it_behaves_like "a sidekiq worker", HardWorkingOperation
end

如果您还需要检查每个工作人员是否使用不同的参数集完成了call,我想您可以将其作为散列传递。但是到了那个时候,你应该问问自己,这个规范是否真的应该被提取出来:

代码语言:javascript
复制
shared_examples_for "a sidekiq worker" do |operation_klass, ops_args|
  ..
  expect(operation_klass).to receive(:call).with(ops_args)
  ..
end

it_behaves_like "a sidekiq worker", HardWorkingOperation, { some_id: some_id }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45097017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档