0

这是我使用 ERB 的代码。它无法绑定 TestCase 对象 ERB 是否适用于对象绑定?TestReport 类中有两个方法。如果您注释 Testcase 对象绑定,则代码按预期工作。如果我尝试绑定对象,则会收到以下错误。TestCase 的“'get_binding' 中的块:未定义的方法 id”。

#!/usr/bin/ruby

require "erb"enter code here

# Build template data class.
class TestcaseReport

   def initialize(platform)
   @platform = platform
   @testcases = [ ]
   @features = [ ]
   end

  def add_testcase(testcase)
    @testcases << testcase
  end

   def add_feature(feature)
    @features << feature
  end

    # Support templating of member data.
  def get_binding
    binding
  end
end


class TestCase
  def initialize(id, link, name, testcasetype, time, status )
    @id = id
    @link = link
    @name = name
    @testcasetype = testcasetype
    @time = time
    @status = status
  end
end

# Create template.
template = %{
   <html>
      <head>
        <title>BreatheMapper Test Execution Report</title>
        <style>
        * {
          box-sizing: border-box;
        }

        /* Create columns that floats next to each other */
        .column40 {
          float: left;
          margin: auto;
          width: 40%;
        }

        .column25 {
          float: left;
          margin: auto;
          width: 25%;
        }

        .column13 {
          float: left;
          margin: auto;
          width: 13%;
        }

        .column9 {
          float: left;
          margin: auto;
          width: 9%;
        }

        /* Clear floats after the columns */
        .row:after {
          content: "";
          display: table;
          clear: both;
        }
        div {
          border-style: solid;
          border-color: #ADD470;
          border-width: 1px 1px 0px 1px;
          height: auto;
        }
        </style>
      </head>
      <body style="color:#4A4A4A;">

        <h2 align="center">BretheMapper Android Test Execution Report</h2>

        <p><b>Executed By:&nbsp;</b>Manish Rathi<br/>
          <b>Build Version Number:&nbsp;</b>0.3.0-alpha-20182006<br/>
          <b>Test Execution Date Time:&nbsp;</b>2018-06-26 16:53:17 +0530<br/>
          <b>Test Executed on Git Revision:&nbsp;</b><a href="https://scm.sapphirepri.com/Mapper/BreatheMapperIos/tree/9778827">9778827</a></p>
          <h5 style="color:#467FBB;">Executed 59 Component tests  in 2.776 seconds. <br/>
          Executed 1 Integration test(s)  in 27.2 seconds. </h5>


       <% @features.each do |f| %>
          <div class="row">
          <div class="column25" style="background-color:#C2E791;">
            <p style="line-height:0px;">&nbsp;<a href="https://share.careorchestrator.com/display/BMT/Scenario+2690.18852.1+%3A+TC4367"><%= f %></a></p>
          </div>
          <div class="column40" style="background-color:#C2E791;">
            <p style="line-height:0px;">&nbsp;<a href="https://scm.sapphirepri.com/Mapper/BreatheMapperIos/tree/develop/Source/BreatheMapper/BreatheMapperTests/Login/LoginViewControllerTests+V&V.swift#L18"><%= f %></a></p>
          </div>
          <div class="column13" style="background-color:#C2E791;">
            <p align="center" style="line-height:0px;">&nbsp;<%= f %></p>
          </div>
          <div class="column13" style="background-color:#C2E791;">
            <p align="center" style="line-height:0px;">&nbsp;<%= f %></p>
          </div>
          <div class="column9" style="background-color:#C2E791;">
            <p align="center" style="line-height:0px;">&nbsp;<%= f %></p>
          </div>
           </div>
      <% end %>

       <% @testcases.each do |t| %>
          <div class="row">
          <div class="column25" style="background-color:#C2E791;">
            <p style="line-height:0px;">&nbsp;<a href="https://share.careorchestrator.com/display/BMT/Scenario+2690.18852.1+%3A+TC4367"><%= t.id %></a></p>
          </div>
          <div class="column40" style="background-color:#C2E791;">
            <p style="line-height:0px;">&nbsp;<a href="https://scm.sapphirepri.com/Mapper/BreatheMapperIos/tree/develop/Source/BreatheMapper/BreatheMapperTests/Login/LoginViewControllerTests+V&V.swift#L18"><%= t.name %></a></p>
          </div>
          <div class="column13" style="background-color:#C2E791;">
            <p align="center" style="li`enter code here`ne-height:0px;">&nbsp;<%= t.link %></p>
          </div>
          <div class="column13" style="background-color:#C2E791;">
            <p align="center" style="line-height:0px;">&nbsp;<%= t.time %></p>
          </div>
          <div class="column9" style="background-color:#C2E791;">
            <p align="center" style="line-height:0px;">&nbsp;<%= t.status %></p>
          </div>
           </div>
      <% end %>







</body>
</html>
}.gsub(/^  /, '')

rhtml = ERB.new(template)
toy = TestcaseReport.new( "Android" )

toy.add_feature("Listens for verbal commands in the Ruby language!")
toy.add_feature("Ignores Perl, Java, and all C variants.")
toy.add_feature("Karate-Chop Action!!!")
toy.add_feature("Matz signature on left leg.")
toy.add_feature("Gem studded eyes... Rubies, of course!")

toy.add_testcase(TestCase.new("1","sfdf","sfsfew","swrwrr","sfsdfsdf","qiqiq"))
toy.add_testcase(TestCase.new("2","sfdf","sfsfew","swrwrr","sfsdfsdf","qiqiq"))

toy.add_testcase(TestCase.new("3","sfdf","sfsfew","swrwrr","sfsdfsdf","qiqiq"))

toy.add_testcase(TestCase.new("4","sfdf","sfsfew","swrwrr","sfsdfsdf","qiqiq"))

toy.add_testcase(TestCase.new("5","sfdf","sfsfew","swrwrr","sfsdfsdf","qiqiq"))

# Produce result.
rhtml.run(toy.get_binding)


File.open('testcase.html', 'w') do |f|
  f.write rhtml.result toy.get_binding
end
4

1 回答 1

0

出现您的问题是因为您的TestCase类没有id实例方法。要创建此方法,您可以简单地添加attr_reader :id到您的类中。

class TestCase
  attr_reader :id

  def initialize(id, link, name, testcasetype, time, status )
    @id = id
    @link = link
    @name = name
    @testcasetype = testcasetype
    @time = time
    @status = status
  end
end

通过添加这个,调用时代码不会出错<%= t.id %>

于 2018-07-09T14:25:43.633 回答