heinztomato
heinztomato

Reputation: 825

Basic introduction to Ruby Minitest

I am using minitest for the first time and I am having trouble understanding how to write my first test method. Can anyone assist in helping me understand what I should be testing in the below Player method get_name?

class Player
  def get_name(player)
    puts `clear`
    center("#{player}, whats your name bro/ladybro?")
    @name = gets.chomp
    until @name =~ /\A[[:alnum:]]+\z/
      center("you can do a combination of alphanumeric characters")
      @name = gets.chomp
    end
  end
end

This is what I have in my test file, I was thinking I am just suppose to test the regex to make sure it takes alpha and numeric characters.

class TestPlayer < Minitest::Test
  def test_get_name
    describe "get_name" do
      it "should allow an input of alphanumeric characters" do
        assert_match(/\A[[:alnum:]]+\z/, "test_string123")
      end
    end
  end
end

but when I run the tests, nothing seems to happen, I would imagine I am suppose to have 1 assertion.

Run options: --seed 10135

# Running:

.

Finished in 0.001565s, 638.9776 runs/s, 0.0000 assertions/s.

1 runs, 0 assertions, 0 failures, 0 errors, 0 skips

can anyone assist in demonstrating how I should write a test for this scenario? thanks.

Upvotes: 0

Views: 301

Answers (1)

Jikku Jose
Jikku Jose

Reputation: 18804

Minitest test may be described as follows (Assertion syntax):

  1. Its just a simple Ruby file which has a class thats typically a subclass of Minitest::Test.
  2. The method setup will be called at the first; you can define objects that you may require in every test. Eg: Consider assigning an instance of the Player object here in an instance variable in setup method so that you can use it elsewhere in the test class.
  3. A test is defined inside a method that starts with the string: test_; any other method can be used to reduce duplication of code but it won't be considered part of tests.
  4. Typically you should think about testing the return value of a method you want to test.
  5. Testing a method with external input is more convoluted, I would suggest to start with testing methods that have testable output.

Upvotes: 1

Related Questions