Rubocop:模糊块运算符。可以通过不同的方式编写块变量(&init)来通过此cop吗?

问题描述 投票:0回答:1

我正在尝试测试遵循构建器模式的类。

构造函数屈服于配置块。

我收到此RuboCop违规,但不确定如何解决?

这是引起问题的代码。

subject { described_class.build &init }

歧义块运算符。括号中的方法参数如果肯定是一个块运算符,或在&的右侧添加一个空格(如果它应为二进制AND)。 (警告:Lint / AmbiguousOperator)

  # Builder pattern:
  # Separate the construction of a complex object from its representation
  # so that the same construction process can create different representations.
  module Builder
    class DocumentBuilder
      attr_reader :document

      def initialize
        @document = SomeGem::Document.new
      end

      def self.build
        builder = new
        yield(builder)
        builder.document
      end
    end
  end
RSpec.describe Builder::DocumentBuilder do
  subject { described_class.build &init }
  let(:init) { ->(_builder) { puts 'do the funcky monkey' } }

  describe 'build' do
    context 'no data' do
      it { is_expected.to be_a(SomeGem::Document) }
    end
  end
end
# Rspec Output
# build
#    no data
# do the funcky monkey
#       is expected to be a kind of SomeGem::Document
ruby rspec rubocop
1个回答
0
投票

主题{describe_class.build(&init)已解决}

© www.soinside.com 2019 - 2024. All rights reserved.