Похоже, вы должны использовать общие примеры RSpec. Поднятый из другого моего ответа, ниже приведен пример, который должен быть самоочевидным и демонстрировать различные способы прохождения аргументов с использованием блока let.
Блок shared_examples наверху является эквивалентом метода , если хотите, а остальные примеры, демонстрирующие использование этого блока shared_examples.
require 'rails_helper'
RSpec.describe Array, type: :class do
shared_examples 'an array of hashes' do
it { expect(array).to be_an_instance_of(Array) }
it 'each element should be an instance of Hash' do
array.each { |element| expect(element).to be_an_instance_of(Hash) }
end
end
describe 'with an array of hashes' do
context 'with predefined array' do
let(:hash) { Hash.new(name: 'hash', value: 'value') }
let(:array) { [hash, hash, hash] }
context 'without using shared examples' do
it { expect(array).to be_an_instance_of(Array) }
it 'each element should be an instance of Hash' do
array.each { |element| expect(element).to be_an_instance_of(Hash) }
end
end
context 'using shared examples' do
it_should_behave_like 'an array of hashes'
end
end
context 'when passing array to shared example' do
let(:hash) { Hash.new(name: 'hash', value: 'value') }
let(:myarray) { [hash, hash, hash] }
it_should_behave_like 'an array of hashes' do
let(:array) { myarray }
end
context 'with use of before(:each) block' do
before(:each) do
@myarray = myarray
end
it_should_behave_like 'an array of hashes' do
let(:array) { @myarray }
end
end
end
end
end
Это должно сработать. Нам понадобится [mcve], чтобы точно сказать, что здесь не так. Кроме того, кажется, что вы хотите использовать rspec [общие примеры] (https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples). –
Вы должны изучить общие примеры. – David
Мне кажется, что вы пытаетесь повторно реализовать [общие примеры] (https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples). – spickermann