2017-05-02 27 views
-1

我以前從未使用過Ruby,並且試圖運行一個很早以前寫入 的程序。我已經安裝了Ruby 2.4.1和gem包[測試單元 3.4.3] OK,但是當我嘗試運行它,我得到一個錯誤:正式參數不能是ruby中的類變量

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

有什麼特別I」米做錯了嗎? 下面的代碼片段:

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 

在此先感謝

+2

只需使用塊內的局部變量:'@@ tc_array.each do | tc | ... end' – Ilya

+4

非常抱歉,您必須從此代碼學習ruby ... –

+0

@Ilya,實際上,此代碼與Ruby-1.8.7一起工作良好。現在我正在檢查Ruby-2.4.1。這些版本中的賦值語義有沒有改變? –

回答

0

這是使「TC」爲局部變量之後,現在固定。