0

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?

mr_muscle
  • 2,536
  • 18
  • 61

1 Answers1

2

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
knut
  • 27,320
  • 6
  • 84
  • 112
  • Ok but why I can't use `test '(...)' do ` syntax ? what does it depend on? BTW your code raise an error `UkTest#test_remove_whitespaces: NameError: uninitialized constant UkTest::Formatter` – mr_muscle Oct 05 '21 at 22:22
  • For the error: Do you load your code in `uk_test.rb`? (Depending on your settings `require uk.rb` or `require_relative uk.rb`) - I added a full example with the complete code in one file. – knut Oct 05 '21 at 22:48
  • Ok I've to add this file based on a very strange way - `require_relative '../../../../lib/formatter/phone_number/uk'`, is there any workaround to avoid this stupid `../../../../` ? – mr_muscle Oct 05 '21 at 23:02
  • Why do you expect the `test '(...)' do` syntax should work? I may be wrong, but I never saw this syntax in combination with minitest. Maybe it would be worth for you, to take a look to rspec or minitest/spec. I will add a little code example. – knut Oct 05 '21 at 23:02
  • for your `require_relative`-question: It depends on you folder structure. You could also extend the loadpath `$LOAD_PATH` (see e.g. https://stackoverflow.com/questions/5177648/ruby-adding-a-directory-to-load-path-what-does-it-do). – knut Oct 05 '21 at 23:14
  • `test ' 'do` is standard syntax for Minitest, nothing related with `minitest/spec` https://guides.rubyonrails.org/testing.html#rails-meets-minitest check the section with `test "the truth" do `. I don't know but maybe it works only when Rails is on board - my app is pure Ruby app. – mr_muscle Oct 05 '21 at 23:17
  • @mr_muscle Yes, it looks like the `test '...' do ... end` syntax is an extension to minitest that Rails adds. – Code-Apprentice Oct 06 '21 at 15:52