为什么我的Ruby代码中出现“错误的参数数量(0表示2)”异常?

时间:2021-10-10 20:19:17

I'm trying to polish up my Ruby by re writing Kent Beck's xUnit Python example from "Test Driven Development: By Example". I've got quite far but now I get the following error when I run which I don't grok.

我试图通过从“测试驱动开发:通过示例”编写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

My code looks like this:

我的代码如下所示:

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

Can anyone point out my obvious mistake?

任何人都可以指出我明显的错误吗?

2 个解决方案

#1


11  

It's your print statement:

这是你的印刷声明:

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

should be

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

without the '@' you are calling Kernel#test, which expects 2 variables.

没有'@'你正在调用Kernel#test,它需要2个变量。

#2


0  

One thing that leaps out is that the send method expects a symbol identifying the method name, but you're trying to use an instance variable.

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

Object.send documentation

Also, shouldn't lines like this:

此外,不应该像这样的行:

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

be:

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

?

#1


11  

It's your print statement:

这是你的印刷声明:

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

should be

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

without the '@' you are calling Kernel#test, which expects 2 variables.

没有'@'你正在调用Kernel#test,它需要2个变量。

#2


0  

One thing that leaps out is that the send method expects a symbol identifying the method name, but you're trying to use an instance variable.

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

Object.send documentation

Also, shouldn't lines like this:

此外,不应该像这样的行:

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

be:

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

?