2008-09-22 22 views
3

我想通过从“测试驱动开发:通过示例”编写Kent Beck的xUnit Python示例来改进我的Ruby。我有相当远的距离,但现在当我跑步的时候,我得到了下面的错误,我不理睬。为什么我的Ruby代码中出现“错误的参数数量(0代表2)”异常?

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 

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

回答

11

这是你的打印语句:

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

应该

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

没有 '@' 你调用内核#测试,预计2个变量。

+0

是的,应该这样做。我试过了,它按预期工作。 – 2008-09-22 20:00:45

0

有一点需要注意的是,send方法需要一个标识方法名称的符号,但是您正尝试使用一个实例变量。

Object.send documentation

而且,不应线是这样的:

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

是:

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

+0

发送将接受字符串和符号,在他的情况下,实例变量包含他想要的字符串。没关系:-) – 2008-09-23 04:06:23

相关问题