Ruby コードで「引数の数が間違っています (2 に対して 0)」という例外が発生するのはなぜですか?
-
02-07-2019 - |
質問
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
誰か私の明らかな間違いを指摘してもらえますか?
解決
これは print ステートメントです。
puts "test was run? (true expected): #{test.wasRun}"
あるべきです
puts "test was run? (true expected): #{@test.wasRun}"
「@」を付けないと、2 つの変数を必要とする Kernel#test を呼び出していることになります。
他のヒント
一つ飛び出てくるのは、 send
メソッドにはメソッド名を識別するシンボルが必要ですが、インスタンス変数を使用しようとしています。
また、次のような行は使用しないでください。
puts "test was run? (true expected): #{test.wasRun}"
なれ:
puts "test was run? (true expected): #{@test.wasRun}"
?
所属していません StackOverflow