-
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.
Automated testing options
-
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'
- add the require for the class under test
- files are named after the class under test ie:
-
tests can help describe the expected behaviour of an app
Inheritance
- Inheritance a common pattern when using frameworks
- we will use it a lot when using Rails
- more next week when we look at the MVC design pattern
Example where we used inheritance
class ProductTest < MiniTest::Test
.
Class method call
StringValidator.valid_email(email)
In the following example
- class method declaration
- const variable in a class (freeze is a rubocop tip)
# class to hide the regex details
class StringValidator
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.freeze
def self.valid_email(email_string)
email_string =~ VALID_EMAIL_REGEX
end
end
- not necessarily something you will need to use very often
- good to know they exist and you kind find details on how to use them online
- online regex verifier https://rubular.com/
- online strftime generator http://strftimer.com/