代码之家  ›  专栏  ›  技术社区  ›  Kamilski81

如何避免每次在rspec中输入模块名?

  •  0
  • Kamilski81  · 技术社区  · 6 年前

    今天的代码:

    ocr_text = 'Jim Baker & Co'
    recipient_matches = [
    RecipientMatcher::RecipientMatchFound.new(build(:group), RecipientMatcher::MatchObject.new(nil,'Jim'), ocr_text, ocr_text.match(/Jim/)),
    RecipientMatcher::RecipientMatchFound.new(build(:group), RecipientMatcher::MatchObject.new(nil, 'Jim Baker & Co'), ocr_text, ocr_text.match(/Jim Baker & Co/)),
    RecipientMatcher::RecipientMatchFound.new(build(:group), RecipientMatcher::MatchObject.new(nil, 'Jim Baker'), ocr_text, ocr_text.match(/Jim Baker/)),
    ]
    
    section_meta_data = RecipientMatcher::PossibleRecipientsCalculator.determine_primary_recipients_in_section(recipient_matches)
    primary_recipients = section_meta_data.primary_recipients
    
    expect(true).to eq(true)
    

    如果我能写下这样的话,那就太理想了:

    RecipientMatcher.magic_method do
        ocr_text = 'Jim Baker & Co'
        recipient_matches = [
        ::RecipientMatchFound.new(build(:group), ::MatchObject.new(nil,'Jim'), ocr_text, ocr_text.match(/Jim/)),
        ::RecipientMatchFound.new(build(:group), ::MatchObject.new(nil, 'Jim Baker & Co'), ocr_text, ocr_text.match(/Jim Baker & Co/)),
        ::RecipientMatchFound.new(build(:group), ::MatchObject.new(nil, 'Jim Baker'), ocr_text, ocr_text.match(/Jim Baker/)),
        ]
    
        section_meta_data = ::PossibleRecipientsCalculator.determine_primary_recipients_in_section(recipient_matches)
        primary_recipients = section_meta_data.primary_recipients
    
        expect(true).to eq(true)
    end
    
    4 回复  |  直到 6 年前
        1
  •  2
  •   koffeinfrei    6 年前

    你可以用RSpec的 described_class ,前提是提供类作为main的参数 describe 块,如下所示:

    RSpec.describe RecipientMatcher::RecipientMatchFound do
      it 'whatever' do
        described_class.new(...)
      end
    end
    

    let 在RSpec)中,例如:

    let(:match_found) { RecipientMatcher::RecipientMatchFound }
    let(:match_object) { RecipientMatcher::MatchObject }
    
    it 'whatever' do
      match_found.new(build(:group), match_object.new(...))
    end
    
        2
  •  2
  •   Andrew Schwartz    6 年前

    一直以来,我都会说把它写出来。打字多一点,但读起来很容易。

    方法1:通过将模块分配给变量来缩短它

    rm = RecipientMatcher
    rm::RecipientMatchFound.new(...)
    ...
    

    或者进一步深入到单个嵌套类

    match_found = RecipientMatcher::RecipientNotFound
    match_obj = RecipientMatcher::MatchObject
    
    match_found.new(...)
    

    方法2:为每个常用的段用helper方法包装您的构造

    def build_match(str)
      RecipientMatcher::RecipientMatchFound.new(build(:group), RecipientMatcher::MatchObject.new(nil,str), ocr_text, ocr_text.match(/#{str}/))
    end
    
    recipient_matches = [
      build_match('Jim'),
      build_match(...),
      ...
    ]
    

    或者在循环中

    recipient_matches = ['Jim', 'Baker & Co', ...].map{|str| build_match(str)}
    
        3
  •  1
  •   tadman    6 年前

    您可以随时创建别名,这非常简单:

    RecipientMatchFound = RecipientMatcher::RecipientMatchFound
    MatchObject = RecipientMatcher::MatchObject
    
    ocr_text = 'Jim Baker & Co'
    recipient_matches = [
      RecipientMatchFound.new(build(:group), MatchObject.new(nil,'Jim'), ocr_text, ocr_text.match(/Jim/)),
      RecipientMatchFound.new(build(:group), MatchObject.new(nil, 'Jim Baker & Co'), ocr_text, ocr_text.match(/Jim Baker & Co/)),
      RecipientMatchFound.new(build(:group), MatchObject.new(nil, 'Jim Baker'), ocr_text, ocr_text.match(/Jim Baker/)),
    ]
    
    section_meta_data = RecipientMatcher::PossibleRecipientsCalculator.determine_primary_recipients_in_section(recipient_matches)
    primary_recipients = section_meta_data.primary_recipients
    
    expect(true).to eq(true)
    

    尽管重构得更多一些,但通过应用名为 别再说了 或干燥:

    recipient_matches = [
      'Jim',
      'Jim Baker & Co',
      'Jim Baker'
    ].map do |name|
      RecipientMatchFound.new(
        build(:group),
        MatchObject.new(nil, name), ocr_text, ocr_text.match(name)
      )
    end
    

    试着从数据转换的角度考虑Ruby程序。您通常可以从一些简单的东西开始,比如这个名称数组,然后逐步构建出您想要的结构。

        4
  •  0
  •   Ilya Konyukhov    6 年前

    只是个主意。而不是

    RecipientMatcher::RecipientMatchFound
    

    magic_resolver('RecipientMatchFound')
    

    如果定义为RSpec助手:

    def magic_resolver(class_name)
      "RecipientMatcher::#{class_name}".constantize
    end