【问题标题】:formal argument cannot be a class variable in ruby形式参数不能是 ruby​​ 中的类变量
【发布时间】:2017-05-02 11:29:39
【问题描述】:

我以前从未使用过 Ruby,并且正在尝试运行一个编写的程序 很久以前。我已经安装了 Ruby 2.4.1 和 gem 包 [test-unit 3.4.3] 好的,但是当我尝试运行它时,我得到一个错误:

tcreporter.rb:156: formal argument cannot be a class variable
  @@tc_array.each do |@@tc|
                          ^

有什么特别是我做错了吗? 下面是代码 sn-p :

class TCReporter
  @@m = nil; @@c = nil; @@tc = nil; @@status = 'p'; @@notes = nil; @@tlArr = []
  @@resultMapping = {'p'=>'PASS', 'f'=>'FAIL', 'b'=>'BLOCKED', 's'=>'SKIP','pr'=>'PREQFAIL', 'con'=>'CONERR', 'h'=>'HWSKIP', 'cor'=>'CORE'}

  def self.report_result(currentTest, status, notes=nil)
    if $trInit
      @@m = currentTest.split('(')[0] #@@m is the test METHOD currently being executed in the framework.
      @@c = currentTest.split('(')[1].split(')')[0] #@@c is the test CLASS currently being executed in the framework
      if @@c =~ /(?:#{TESTRAIL_TEST_PREFIXES.join('|')})_\d*$/i #If there's a mapping on the test class then report a test class result.
          @@tc = @@c.scan(/(?:#{TESTRAIL_TEST_PREFIXES.join('|')})_\d*$/i)[0].upcase.sub('_', '-') #Get the TR class mapping
          #When reporting at the test class level, the status always starts out as 'p' (PASS). If there's any
          #non-passing status for any test method within the test class (blocked or failed) then use that result
          #for reporting. Once the global status '@@status' has been updated once then no more updating occurs.
          if @@status == 'p' && status != 'p'
            @@status = status
            @@notes = notes
      end
      if eval("#{@@c}.public_instance_methods.grep(/^test_/).sort.first") == @@m && eval("#{@@c}.public_instance_methods.grep(/^test_/).sort.last") == @@m #The first test method is the last test method. All done, do a TestLink update.
        begin
          result = TR.report_tc_result(@@tc, TESTRAIL_PROJECT, TESTRAIL_MILESTONE, TESTRAIL_PLAN, TESTRAIL_RUN, TESTRAIL_BUILD, @@status, (@@notes ? @@notes : notes))
        ensure
          result.case_id = @@tc
          result.class = @@c
          result.method = @@m
          if !result.success #success means a successful communication with testLink and test case was found and updated.
            $trReport = ReportFile.new('tr_report.txt') if !$trReport
            $trReport.puts "#{@@tc}, #{TEST_ARGS.project}, #{TEST_ARGS.plan}, #{TEST_ARGS.build}, #{TEST_ARGS.platform}, #{@@c}, #{@@m}, #{status} 'class', #{result.message ||= result.exception}"
          end
        end
      elsif eval("#{@@c}.public_instance_methods.grep(/^test_/).sort.first") == @@m #A new test class is being evaluated. Set everything to default except status (use whatever the first test class returned).
        @@m = nil; @@c = nil; @@tc = nil; @@status = status
      elsif eval("#{@@c}.public_instance_methods.grep(/^test_/).sort.last") == @@m #Done with the test class. Time to report the test result.
        begin
          result = TR.report_tc_result(@@tc, TESTRAIL_PROJECT, TESTRAIL_MILESTONE, TESTRAIL_PLAN, TESTRAIL_RUN, TESTRAIL_BUILD, @@status, (@@notes ? @@notes : notes))
        ensure
          result.case_id = @@tc
          result.class = @@c
          result.method = @@m
          if !result.success #success means a successful communication with testLink and test case was found and updated.
            $trReport = ReportFile.new('tr_report.txt') if !$trReport
            $trReport.puts "#{@@tc}, #{TEST_ARGS.project}, #{"#{TEST_ARGS.milestone}, " if TEST_ARGS.milestone}#{TEST_ARGS.plan}, #{TEST_ARGS.build}, #{TEST_ARGS.platform}, #{@@c}, #{@@m}, #{status} 'class', #{result.message ||= result.exception}"
          end
        end
      else #The test class is still being executed. Don't update TestLink yet, just check for a non-passing result.
        if @@status == 'p' && status != 'p' #Update the test status if it's a non-pass result. Otherwise, use the earlier failed or blocked status.
          @@status = status
        end
      end
      end
      #If there's a mapping on a test method then report a test method result.
      if @@m =~ /(?:#{TESTRAIL_TEST_PREFIXES.join('|')})_\d?[\d_]+$/i
          @@tc_array = @@m.scan(/(?:#{TESTRAIL_TEST_PREFIXES.join('|')})_\d?[\d_]+$/i)[0].upcase.sub('_', '-').split("_")
          if @@tc_array.size > 1
            tmp_prefix = @@tc_array[0].split("-").first
            tmp_array = []
            @@tc_array.each do|tmp|
              tmp_array << tmp_prefix + "-" + tmp.split("-").last
            end
            @@tc_array = tmp_array
      end
      @@tc_array.each do |@@tc|
        begin
          result = TR.report_tc_result(@@tc, TESTRAIL_PROJECT, TESTRAIL_MILESTONE, TESTRAIL_PLAN, TESTRAIL_RUN, TESTRAIL_BUILD, status, notes)
          puts status
        rescue => e
          puts e
        ensure
          if result && !result.success
            $trReport = ReportFile.new('tr_report.txt') if !$trReport
            $trReport.puts "#{@@tc}, #{TEST_ARGS.project}, #{"#{TEST_ARGS.milestone}, " if TEST_ARGS.milestone}#{TEST_ARGS.plan}, #{TEST_ARGS.build}, #{TEST_ARGS.platform}, #{@@c}, #{@@m}, #{status} 'class', #{result.message ||= result.exception}"
          end
        end
      end
    end
  end
end

提前致谢

【问题讨论】:

  • 只使用块内的局部变量:@@tc_array.each do |tc| ... end
  • 很抱歉你必须从这段代码中学习 ruby​​...
  • @TarunRawat 是的,有很多这样的小改动,尤其是在1.8 --> 1.9 --> 2.0 之间。请注意,这些版本不再受支持 - 您应该至少使用 2.2,最好是(最新的)2.4.1
  • 耶稣....我宁愿重写它也不愿尝试“修复”它!写它的人(“很久以前”)显然不知道他们在做什么——你基本上让全局变量到处乱跑。
  • 当你在处理它时,git blame 文件并向他们发送一封非常愤怒的电子邮件。

标签: ruby testunit


【解决方案1】:

现在将“tc”作为局部变量后已修复。

【讨论】:

    猜你喜欢
    • 2011-10-21
    • 1970-01-01
    • 2015-04-29
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多