I'm trying to learn the TDD approach, to do so I'm using pure Ruby app which is responsible for formatting UK phone number. In my class I want to test if the whitespaces and are removed from given phone number. Basically I'm trying to test the Ruby .delete(' ')
method.
Tested module
# lib/formatter/phone_number/uk.rb
module Formatter
module PhoneNumber
module Uk
(...)
def self.format(number)
number.delete(' ')
end
end
end
end
The test
# test/lib/formater/phone_number/uk_test.rb
require 'minitest/autorun'
class UkTest < Minitest::Test
test 'remove whitespaces' do
invalid_number = ' 44 12 12 12 12 '
Formatter::PhoneNumber::Uk.format(invalid_number)
assert_equal ' 4412121212'
end
end
Which give me an error:
test/lib/formater/phone_number/uk_test.rb:6:in `test': wrong number of arguments (given 1, expected 2) (ArgumentError)
Aside from the fact that testing a method built in Ruby is not a good idea, what am I doing wrong?
CodePudding user response:
You must define a test-method (the method name must start with test_
).
Inside the test method you define your assertions. In your case, you compare the expected value with the result of your method.
class UkTest < Minitest::Test
def test_remove_whitespaces
invalid_number = ' 44 12 12 12 12 '
assert_equal ' 4412121212', Formatter::PhoneNumber::Uk.format(invalid_number)
end
end
Full test in one file:
module Formatter
module PhoneNumber
module Uk
def self.format(number)
number.delete(' ')
end
end
end
end
require 'minitest/autorun'
class UkTest < Minitest::Test
def test_remove_whitespaces
invalid_number = ' 44 12 12 12 12 '
assert_equal ' 4412121212', Formatter::PhoneNumber::Uk.format(invalid_number)
end
end
Same test with minitest/spec
require 'minitest/spec'
describe Formatter::PhoneNumber::Uk do
it 'removes spaces from numbers' do
invalid_number = ' 44 12 12 12 12 '
_(Formatter::PhoneNumber::Uk.format(invalid_number)).must_equal(' 4412121212')
end
end