3

我试图通过从“测试驱动开发:示例”中重新编写 Kent Beck 的 xUnit Python 示例来完善我的 Ruby。我已经走了很远,但现在我在运行时收到以下错误,我不知道。

C:\Documents and Settings\aharmel\My Documents\My Workspace\TDD_Book\TDDBook_xUnit_RubyVersion\lib\main.rb:21:in `test_running': wrong number of arguments (0 for 2) (ArgumentError)
    from C:\Documents and Settings\aharmel\My Documents\My Workspace\TDD_Book\TDDBook_xUnit_RubyVersion\lib\main.rb:21:in `run'
    from C:\Documents and Settings\aharmel\My Documents\My Workspace\TDD_Book\TDDBook_xUnit_RubyVersion\lib\main.rb:85

我的代码如下所示:

class TestCase
  def initialize(name)
    puts "1.  inside TestCase.initialise: @name: #{name}"
    @name = name
  end
  def set_up
    # No implementation (but present to be overridden in WasRun) 
  end
  def run
    self.set_up
    self.send @name  # <<<<<<<<<<<<<<<<<<<<<<<<<= ERROR HERE!!!!!!
  end
end

class WasRun < TestCase
  attr_accessor :wasRun
  attr_accessor :wasSetUp 

  def initialize(name)
    super(name)
  end
  def set_up
    @wasRun = false
    @wasSetUp = true
  end
  def test_method
    @wasRun = true
  end
end

class TestCaseTest < TestCase
  def set_up
    @test = WasRun.new("test_method")
  end
  def test_running
    @test.run
    puts "test was run? (true expected): #{test.wasRun}"
  end
  def test_set_up
    @test.run
    puts "test was set up? (true expected): #{test.wasSetUp}"
  end
end

TestCaseTest.new("test_running").run

谁能指出我明显的错误?

4

2 回答 2

11

这是您的打印声明:

  puts "test was run? (true expected): #{test.wasRun}"

应该

  puts "test was run? (true expected): #{@test.wasRun}"

没有“@”,您调用的是 Kernel#test,它需要 2 个变量。

于 2008-09-22T19:43:20.960 回答
0

跳出来的一件事是该send方法需要一个标识方法名称的符号,但您正在尝试使用实例变量。

Object.send 文档

另外,不应该这样的行:

puts "test was run? (true expected): #{test.wasRun}"

是:

puts "test was run? (true expected): #{@test.wasRun}"

?

于 2008-09-22T18:42:13.117 回答