我写了一份侧写工人说明书。所以现在我们有四名工人,他们的规格几乎相同。所有规范都将对some_method进行测试,并检查任务是否等同。
我的示例员工代码:
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第二个样本规范:
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我想要编写一个单独的规范,测试接收某种方法的所有工作人员(对于不同的方法可以是不同的),并且作业已经排队。我怎么才能做得最好呢?有什么建议吗?
发布于 2017-07-14 07:38:20
您可以使用共享示例。假设它们都有一个将执行call的“操作”类,可能如下所示:
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,我想您可以将其作为散列传递。但是到了那个时候,你应该问问自己,这个规范是否真的应该被提取出来:
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 }https://stackoverflow.com/questions/45097017
复制相似问题