2016.12.03 Testing

I am glad that I finally start to write something that is not OOP. lol

Some common assertion

I will list out some common assertion and how to use it.

  1. assert_eqaul: It should be the most common assertion in testing. The first argument is expected value, and second one is actual value.
write a test to test the size of an array(@list reference an array)
def test_size
assert_equal(3, @list.size)
end

2. assert_raise: it will raise error message when certain kind of situation occur. The following block specify the situation.

write a minitest assertion that will fail unless employee.hire raises a NoExperienceError
def test_add_raise_error  
assert_raises(NoExperienceError) { employee.hire }
end

3. assert_nil: it test whether its first argument is nil.

assert_nil value

4. assert_empty : it test whether its first argument is nil.

assert_empty value

5. assert_include : it test whether its first argument contain second argument.

assert_includes list, 'XYZ'

6. assert_instance_of: check whether the second argument is the object of the class in the first argument.

assert_instance_of Numeric, value

7. assert_kind_of: check whether the second argument is the object of the class and it’s superclass in the first argument.

assert_kind_of Numeric, value

8. assert_same : check the two argument are the same object or not

assert_same list list.process

9. refute_includes: refute act as the opposite of assertion, basically you can find the specific refute by changing the assert to refute, for example: refute_includes opposite to assert_includes.

# Which will return true if the first argument doesn't include the second argument.
refute_includes list, 'xyz'

Summary

Just listed out some common assertion, I will add more thought and useful information in the other days.