'get_binding'未定义的方法“ id”

时间:2018-07-09 14:11:57

标签: ruby

这是我使用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

1 个答案:

答案 0 :(得分: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 %>时代码不会出错