Skip to content

Instantly share code, notes, and snippets.

@leahgarrett
Last active March 28, 2019 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leahgarrett/2b93abed5fc96e8fefac53c80acae3b4 to your computer and use it in GitHub Desktop.
Save leahgarrett/2b93abed5fc96e8fefac53c80acae3b4 to your computer and use it in GitHub Desktop.

minitest

  • testing is a part of ruby best practice

  • manual testing does not scale

  • TDD - Test Driven Development

  • Unit Tests

  • Integration Tests

  • Test Coverage

    • test for behaviour, 100% coverage is not required
  • where to write tests

We have been writing tests with the classes we have written.

city_count = subcountry_city_count(cities, "Australia", "Victoria")
print "ERROR!!! " if city_count != 90
puts("Expect Victoria, Australia to have 90 cities and got #{city_count}")

city_count = subcountry_city_count(cities, "United States", "Florida")
print "ERROR!!! " if city_count != 227
puts("Expect Florida, United States to have 227 and got #{city_count}")

It is a convention to write tests in separate files so the can be run independantly of the applicaiton code.


Automated testing options

  • rspec
  • minitest
    • the default in Rails
    • part of xUnit test family eg: jUnit, nUnit etc
    • uses conventions
      • files are named after the class under test ie: class_name_test.rb
      • tests are named after the method they are testing test_method_name
      • Rails scripts will run all files that end in _test.rb
      • add the require for the monitest gem at the top of the test file require 'minitest/autorun'
      • dd the require for the class under test

Without Rails minitest needs to be installed

gem install minitest

New ruby syntax: Inheritance

  • Inheritance a common pattern when using frameworks
  • we will use it a lot when using Rails
require 'minitest/autorun'
require_relative 'product'

class ProductTest < MiniTest::Test
    def setup
        @product = Product.new({
            :name => 'Widgit',
            :price => '10.99'
        })
    end

    def test_name
        assert_equal 'Widgit', @product.name
    end

    def test_price
        assert_equal 10.99, @product.price
    end
end

Challenges

  • product_test.rb
    • implement the Product class

  • aussie_date_test.rb
    • instructions in the file
    • class that has Australian formats for dates. This class would provide a central place in an application for date formatting. This would consistant formatting is used throughout an application

  • string_validator_test.rb
    • instructions in the file
    • class that contains regex validation. Another centralised utility class that would help ensure consistancy.

Beast

  • refactor your Person class and tests from yesterday
    • create a People class that contains an array of Person objects and all the methods that work on the array
    • review the design with a neighbour and / or teacher
    • create a minitest PeopleTest class and add tests using the test code you wrote
    • create a minitest PersonTest class and add tests
require 'minitest/autorun'
require_relative 'aussie_date'
class AussieDateTest < MiniTest::Test
def setup
@aussie_date = AussieDate.new(2019,5,4)
end
def test_formal_formatted_date
assert_equal 'Saturday, May 4th, 2019', @aussie_date.formal_formatted_date
end
def test_short_formal_formatted_date
assert_equal 'May 4th, 2019', @aussie_date.test_short_formal_formatted_date
end
def test_short_date
assert_equal '4/5/2019', @aussie_date.short_date
end
end
# Create a class for AussieDate
# Keep adding code to create and implement methods until all the tests pass
# Hint methods will look something like:
# def formal_formatted_date
# @date.strftime("%A %B")
# end
# Beast
# Add more format options to the AussieDate class. For each one
# - add the test
# - add the implementation
require 'minitest/autorun'
require_relative 'product'
class ProductTest < MiniTest::Test
def setup
@product = Product.new({
:name => 'Widgit',
:price => '10.99'
})
end
def test_name
assert_equal 'Widgit', @product.name
end
def test_price
assert_equal 10.99, @product.price
end
end
require 'minitest/autorun'
require_relative 'string_validator'
class StringValidatorTest < MiniTest::Test
def setup
@valid_emails = ['dkrishna@att.net',
'bryanw@comcast.net',
'muzzy@hotmail.com',
'jdray@live.com',
'monopole@yahoo.ca']
@invalid_emails = ['anicolaoyahoo.com',
'ournews@outlook',
'me.com',
'schwaang',
'ateniese@macc']
end
def test_email_valid
failure_message = "Failed when checking expected VALID email: "
@valid_emails.each do |email|
assert(StringValidator.valid_email(email) == true, failure_message + email)
end
failure_message = "Failed when checking expected INVALID email: "
@invalid_emails.each do |email|
assert(StringValidator.valid_email(email) == false, failure_message + email)
end
end
end
# Create a class for StringValidator
# Keep adding code to create and implement methods until all the tests pass
# Hint: use regex https://stackoverflow.com/questions/22993545/ruby-email-validation-with-regex
# Beast
# Add more validators to the StringValidator class. For each one
# - add the test
# - add the implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment