views:

1497

answers:

2

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.

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?

A: 

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.

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}"

?

John Topley
Send will accept both strings and symbols, and in his case, the instance variable contains the string he wants. It's fine :-)
Orion Edwards
+9  A: 

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.

AShelly
Yep, that should do it. I tried it and it works as expected.
PJ
Cheers dude. I just got there myself (I was offline for 2 days)
Andrew Harmel-Law