Perché ricevo un numero errato di argomenti (0 per 2) & # 8221; eccezione nel mio codice Ruby?
-
02-07-2019 - |
Domanda
Sto provando a ripulire il mio Ruby scrivendo l'esempio xUnit Python di Kent Beck da & Test; Test Driven Development: By Example " ;. Sono andato abbastanza lontano, ma ora ho il seguente errore quando corro, che non 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
Il mio codice è simile al seguente:
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
Qualcuno può sottolineare il mio evidente errore?
Soluzione
È la tua dichiarazione di stampa:
puts "test was run? (true expected): #{test.wasRun}"
dovrebbe essere
puts "test was run? (true expected): #{@test.wasRun}"
senza '@' stai chiamando il kernel # test, che prevede 2 variabili.
Altri suggerimenti
Una cosa che salta fuori è che il metodo send
prevede un simbolo che identifica il nome del metodo, ma stai cercando di usare una variabile di istanza.
Documentazione di invio oggetto
Inoltre, le linee non dovrebbero essere così:
puts "test was run? (true expected): #{test.wasRun}"
essere:
puts "test was run? (true expected): #{@test.wasRun}"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow