mr_muscle
mr_muscle

Reputation: 2900

Ruby Minitest simple test raise error - wrong number of arguments error

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?

Upvotes: 0

Views: 757

Answers (1)

knut
knut

Reputation: 27845

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

Upvotes: 2

Related Questions